CosminVarlan

Versus: QUICK, MERGE, HEAP sortings

Mar 26th, 2026
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 167.92 KB | None | 0 0
  1. #include <iostream>/// pivotul il consider ca fiind pe ultima pozitie: a[n-1]
  2.     int i = -1;
  3.     for(int j=0; j<n-1; j++)
  4.     {
  5.         if (a[j]<a[n-1])
  6.         {
  7.             i++;
  8.             swap(a[i],a[j]);
  9.         }
  10.     }
  11.     i++;
  12.     swap(a[i], a[n-1]);
  13.     return i;
  14. }
  15.  
  16.  
  17. void quickSort(int a[], int n){
  18.     swap(a[n/2], a[n-1]); /// daca sirul are probabilitate mare sa fie deja sortat
  19.     if (n>1)
  20.     {
  21.         int pozPivot = partitionare(a, n);
  22.         quickSort(a,pozPivot);
  23.         quickSort(a+pozPivot+1, n-pozPivot-1);
  24.     }
  25. }
  26.  
  27.  
  28.  
  29.  
  30. void mergeSort(int s[], int n){   /// 130.000
  31.     if (n==1) return;
  32.     mergeSort(s,n/2); /// imi sorteaza prima jumate
  33.     mergeSort(s+n/2, n-n/2); /// imi sorteaza restul elementelor  (restul poate fi jumate + 1)
  34.  
  35.     int temp[n];
  36.     int p1 = 0, p2 = n/2, p3 = 0;
  37.     while(p1 < n/2 && p2 < n )
  38.     {
  39.         if (s[p1]<s[p2]) temp[p3++] = s[p1++];
  40.                     else temp[p3++] = s[p2++];
  41.     }
  42.     while(p1<n/2) temp[p3++] = s[p1++];
  43.     while(p2<n) temp[p3++] = s[p2++];
  44.     for(int i=0; i<n; i++)
  45.         s[i] = temp[i];
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void SelectionSort2(int a[], int n){
  54.     for(int i=0; i<n-1; i++)
  55.     {
  56.         int minim = i;
  57.         for(int j=i+1; j<n; j++)
  58.             if (a[minim]>a[j]){
  59.                 minim = j;
  60.             }
  61.         int temp = a[i];
  62.         a[i] = a[minim];
  63.         a[minim] = temp;
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.  
  70.     for(int i=0; i<100; i++)
  71.     {
  72.          copiaza(b,a,30000);
  73.          /// quickSort(b,30000); /// 0.247
  74.          /// mergeSort(b,30000); /// 0.224
  75.  
  76.          /// HeapSort(b,30000); /// 0.431
  77.  
  78.          /// SelectionSort2(b,30000); /// 25.000
  79.  
  80.     }
  81.  
  82.     return 0;
  83. }
  84.  
Tags: Sorting Alg
Advertisement
Add Comment
Please, Sign In to add comment