Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>/// pivotul il consider ca fiind pe ultima pozitie: a[n-1]
- int i = -1;
- for(int j=0; j<n-1; j++)
- {
- if (a[j]<a[n-1])
- {
- i++;
- swap(a[i],a[j]);
- }
- }
- i++;
- swap(a[i], a[n-1]);
- return i;
- }
- void quickSort(int a[], int n){
- swap(a[n/2], a[n-1]); /// daca sirul are probabilitate mare sa fie deja sortat
- if (n>1)
- {
- int pozPivot = partitionare(a, n);
- quickSort(a,pozPivot);
- quickSort(a+pozPivot+1, n-pozPivot-1);
- }
- }
- void mergeSort(int s[], int n){ /// 130.000
- if (n==1) return;
- mergeSort(s,n/2); /// imi sorteaza prima jumate
- mergeSort(s+n/2, n-n/2); /// imi sorteaza restul elementelor (restul poate fi jumate + 1)
- int temp[n];
- int p1 = 0, p2 = n/2, p3 = 0;
- while(p1 < n/2 && p2 < n )
- {
- if (s[p1]<s[p2]) temp[p3++] = s[p1++];
- else temp[p3++] = s[p2++];
- }
- while(p1<n/2) temp[p3++] = s[p1++];
- while(p2<n) temp[p3++] = s[p2++];
- for(int i=0; i<n; i++)
- s[i] = temp[i];
- }
- void SelectionSort2(int a[], int n){
- for(int i=0; i<n-1; i++)
- {
- int minim = i;
- for(int j=i+1; j<n; j++)
- if (a[minim]>a[j]){
- minim = j;
- }
- int temp = a[i];
- a[i] = a[minim];
- a[minim] = temp;
- }
- }
- int main()
- {
- for(int i=0; i<100; i++)
- {
- copiaza(b,a,30000);
- /// quickSort(b,30000); /// 0.247
- /// mergeSort(b,30000); /// 0.224
- /// HeapSort(b,30000); /// 0.431
- /// SelectionSort2(b,30000); /// 25.000
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment