Advertisement
nikitakrut58

Test

Feb 26th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <ctime>  
  4. #include<stdio.h>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int SplitArray(int* array, int pivot, int startIndex, int endIndex, int& counter) {
  10.     int leftBoundary = startIndex;
  11.     int rightBoundary = endIndex;
  12.  
  13.     while ((++counter) && (leftBoundary < rightBoundary))
  14.     {
  15.         while (pivot < array[rightBoundary]
  16.             && rightBoundary > leftBoundary)
  17.         {
  18.             rightBoundary--;
  19.         }
  20.         swap(array[leftBoundary], array[rightBoundary]); while ((++counter) && (pivot >= array[leftBoundary])
  21.             && leftBoundary < rightBoundary)
  22.         {
  23.             leftBoundary++;
  24.         }
  25.         swap(array[rightBoundary], array[leftBoundary]);
  26.     }
  27.     return leftBoundary;
  28. }
  29.  
  30. void QuickSort(int* array, int startIndex, int endIndex, int& counter) {
  31.     int pivot = array[startIndex];                  
  32.     int splitPoint;
  33.  
  34.     if (endIndex > startIndex)
  35.     {
  36.         // counter++; // Don't count here
  37.         splitPoint = SplitArray(array, pivot, startIndex, endIndex, counter);
  38.         array[splitPoint] = pivot;
  39.         QuickSort(array, startIndex, splitPoint - 1, counter);  
  40.         QuickSort(array, splitPoint + 1, endIndex, counter);    
  41.     }
  42. }
  43.  
  44.  
  45. void swap(int& a, int& b) {
  46.     int temp;
  47.     temp = a;
  48.     a = b;
  49.     b = temp;
  50. }
  51.  
  52. int main() {
  53.     srand(time(0));
  54.     int* mas = new int[50000];
  55.     int n = 50000,c=0;
  56.  
  57.     for (int i = 0; i < n; i++) {
  58.         mas[i] = rand();
  59.     }
  60.  
  61.     QuickSort(mas, 0, n-1,c);
  62.  
  63.     cout << c;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement