-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
34 lines (29 loc) · 923 Bytes
/
quicksort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "sort.h"
void quicksort(double *arr, int low, int high){
if(low < high){
int i = low, j = high;
double pivot = arr[low];
/* Start screening the array from left for a value greater than pivot.
* Then screen from the right for a value smaller than pivot.
* Then swap them if the larger value is upstream to the smaller value.
* Continue screening and swapping until the whole array is screened.
* Then place the pivot where search for a smaller value stopped.
* The older value therein must be first transfered to the old location of pivot. */
while(i< j){
while(arr[i] <= pivot && i<= high)
i++;
while(arr[j] > pivot && j >= low)
j--;
if(i< j){
double temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[low] = arr[j];
arr[j] = pivot;
// Now once they are divided, conquer them.
quicksort(arr, low, j-1);
quicksort(arr, j+1, high);
}
}