0% found this document useful (0 votes)
57 views

Laborator N1 C

This document discusses sorting algorithms and provides C++ code examples for sorting an integer array in ascending order using: 1) Straight insertion sort 2) Straight selection sort 3) Bubble sort 4) Shaker sort The document instructs the reader to read in a maximum of 20 integer elements into an array, then sort the array using each of the 4 sorting algorithms provided in the C++ code examples.

Uploaded by

Viorica Borta
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Laborator N1 C

This document discusses sorting algorithms and provides C++ code examples for sorting an integer array in ascending order using: 1) Straight insertion sort 2) Straight selection sort 3) Bubble sort 4) Shaker sort The document instructs the reader to read in a maximum of 20 integer elements into an array, then sort the array using each of the 4 sorting algorithms provided in the C++ code examples.

Uploaded by

Viorica Borta
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Laborator 1 Utilizarea tehnicilor de sortare a datelor.

Algoritmi de
sortare intern.

Sarcina: (Obligatoriu)

1. Se consider un vector de maxim 20 elemente de tip ntreg. Numrul de elemente N i

valorile lor se citesc de la terminal. Elaborai un program pentru sortarea ascendent a

acestui masiv liniar utiliznd metodele:

a) sortare prin simpl nserare (Straight insertion);

C++

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()

{int n,a[100],val,j;

cout<<"n = "; cin>>n;


for (int i=0;i<n;i++)
{cout<<"a["<<i<<"] = "; cin>>a[i];}

//straight insertion
for (int i=0;i<n;i++)
{val=a[i]; j=i-1;
while (val<a[j])
{a[j+1]=a[j]; j=j-1;}
a[j+1]=val;}
//end straight insertion

cout<<"Vectorul sortat crescator:\n";


for (int i=0;i<n;i++)
cout << a[i]<<" ";

return 0;

}
b) sortare prin simpl selectare (Straight selection);

C++

#include "stdafx.h"
#include <iostream>
using namespace std;

void selectionSort(int a[], int n)


{int i, j, minIndex, t;
for (i = 0; i<n-1; i++)
{minIndex = i;
for (j = i + 1; j<n; j++)
if (a[j] < a[minIndex])
minIndex = j;
if (minIndex!=i)
{t = a[i]; a[i] = a[minIndex]; a[minIndex] = t; }}}

int main()
{int a[100],n,i;
cout<<"n = \n"; cin>>n;
cout<<"Introduceti elementele vectorului:\n";
for (i=0;i<n;i++) cin>>a[i];
selectionSort(a,n);
cout<<"Vectorul sortat crescator:\n";
for (i=0; i<n; i++) cout<< a[i]<<" ";

return 0;

}
c) sortare prin simplu schimb (Buble sort);

C++

#include "stdafx.h"
#include <iostream>
using namespace std;

void bubbleSort(int a[], int n)


{for(int i=0; i<n; i++)
{for(int j=0; j<n-1; j++)
{if(a[j]>a[j+1])
{int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}}}}

int main()
{int a[100],n,i;
cout<<"n = \n"; cin>>n;
cout<<"Introduceti elementele vectorului:\n";
for (i=0;i<n;i++) cin>>a[i];

bubbleSort(a,n);

cout<<"Vectorul sortat crescator:\n";


for (i=0; i<n ; i++) cout<< a[i]<<" ";

return 0;

}
d) sortare de tipul Shaker. (Shakersort)

C++

void shakerSort(int a[], int n)


{for (int i = 0; i < n/2; i++)
{bool schimb = false;
for (int j = i; j < n - i - 1; j++)
{ //sortarea de la inceput
if (a[j] > a[j+1])
{int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; schimb = true;}}
for (int j = n - 2 - i; j > i; j--)
{ //sortarea de la capat
if (a[j] < a[j-1])
{int tmp = a[j]; a[j] = a[j-1]; a[j-1] = tmp; schimb = true;}}
if(!schimb) break; //daca nu s-au schmbat elemente, inseamnca ca procesul deja s-a
terminat
}}

int main()
{int a[100],n;
cout<<"n = "; cin>>n;
cout<<"Introduceti elementele vectorului:\n";
for (int i=0;i<n;i++) cin>>a[i];

shakerSort(a,n);

cout<<"Vectorul sortat crescator:\n";


for (int i=0; i<n ; i++) cout<< a[i]<<" ";

return 0;

You might also like