Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int v[100];
- void CitireVector( int v[] )
- {
- cin >> n;
- for( int i=1 ; i<=n ; i++ )
- cin >> v[i];
- }
- void AfisareVector( int v[] )
- {
- for( int i=1 ; i<=n ; i++ )
- cout << v[i] << " ";
- }
- void BubbleSort()
- {
- bool gata = false;
- while(!gata)
- {
- gata = true;
- for( int i=1 ; i<n ; i++ )
- if( v[i] > v[i+1] )
- {
- swap(v[i], v[i+1]);
- gata = false;
- }
- }
- }
- void BubbleSortImbunatatit()
- {
- bool gata = false;
- for( int i=0 ; i<n || !gata ; i++ )
- {
- gata = true;
- for( int j=1 ; j<n-i ; j++ )
- if( v[j] > v[j+1] )
- {
- swap(v[j], v[j+1]);
- gata = false;
- }
- }
- }
- void InsertionSort()
- {
- int aux, j;
- for( int i=2 ; i<=n ; i++ )
- {
- aux = v[i];
- j = i-1;
- while( j>=0 && v[j] > aux )
- {
- v[j+1] = v[j];
- j--;
- }
- v[j+1] = aux;
- }
- }
- void SelectionSort(int v[])
- {
- for( int j=1 ; j<n ; j++ )
- {
- int iMinim = j;
- for( int i=j+1 ; i<=n ; i++ )
- if( v[i] < v[iMinim] )
- iMinim = i;
- swap(v[j],v[iMinim]);
- }
- }
- int Cautare( int v[], int caut, int s, int d )
- {
- if(d <= s)
- return (caut > v[s]) ? (s+1) : s;
- int mijloc = (s+d)/2;
- if( caut == v[mijloc] )
- return mijloc;
- if( caut > v[mijloc] )
- return Cautare(v, caut, mijloc+1, d);
- return Cautare(v, caut, s, mijloc-1);
- }
- void BinaryInsertionSort( int v[] )
- {
- int aux, j, loc;
- for( int i=2 ; i<=n ; i++ )
- {
- aux = v[i];
- j = i-1;
- loc = Cautare(v, aux, 1, j);
- while(j >= loc)
- {
- v[j+1] = v[j];
- j--;
- }
- v[j+1] = aux;
- }
- }
- void QuickSort(int v[], int s, int d)
- {
- int i, pivot, j;
- i = s;
- j = d-1;
- pivot = d;
- while( i <= j )
- {
- if( v[i] > v[pivot] )
- {
- swap(v[i],v[j]);
- j--;
- i--;
- }
- i++;
- }
- swap(v[j+1],v[pivot]);
- pivot = j+1;
- if( pivot-1-s > 1 )
- QuickSort(v,s,pivot-1);
- if( d-pivot-1 > 1 )
- QuickSort(v,pivot+1,d);
- }
- int main()
- {
- CitireVector(v);
- SelectionSort(v);
- AfisareVector(v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement