Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <ctime>
- #include<stdio.h>
- using namespace std;
- int SplitArray(int* array, int pivot, int startIndex, int endIndex, int& counter) {
- int leftBoundary = startIndex;
- int rightBoundary = endIndex;
- while ((++counter) && (leftBoundary < rightBoundary))
- {
- while (pivot < array[rightBoundary]
- && rightBoundary > leftBoundary)
- {
- rightBoundary--;
- }
- swap(array[leftBoundary], array[rightBoundary]); while ((++counter) && (pivot >= array[leftBoundary])
- && leftBoundary < rightBoundary)
- {
- leftBoundary++;
- }
- swap(array[rightBoundary], array[leftBoundary]);
- }
- return leftBoundary;
- }
- void QuickSort(int* array, int startIndex, int endIndex, int& counter) {
- int pivot = array[startIndex];
- int splitPoint;
- if (endIndex > startIndex)
- {
- // counter++; // Don't count here
- splitPoint = SplitArray(array, pivot, startIndex, endIndex, counter);
- array[splitPoint] = pivot;
- QuickSort(array, startIndex, splitPoint - 1, counter);
- QuickSort(array, splitPoint + 1, endIndex, counter);
- }
- }
- void swap(int& a, int& b) {
- int temp;
- temp = a;
- a = b;
- b = temp;
- }
- int main() {
- srand(time(0));
- int* mas = new int[50000];
- int n = 50000,c=0;
- for (int i = 0; i < n; i++) {
- mas[i] = rand();
- }
- QuickSort(mas, 0, n-1,c);
- cout << c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement