-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquickSort3Way.c
31 lines (29 loc) · 1023 Bytes
/
quickSort3Way.c
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
#include "exchange.h"
// Worst case performance O(n**2)
// Best case performance O(n)
// Average case performance O(n log n)
void quickSort3Way(long int *array, int start, int end){
if(end > start){
int low = start, great = end;
long int aux, piv = *(array+start), *i = array+start;
while(i-array <= great){ // Set the pivot in the center of the array, sorting greaters and lowers values in the array comparing to pivot
if(*i < piv){ // Compares if the value is lower than pivot
aux = *i; // If so, change the positions of index and low
*i = *(array+low);
*(array+low) = aux;
low++;
i++;
}
else if(*i > piv){ // if the value is greater than pivot
aux = *i; // if so, change the positions of index and great
*i = *(array+great);
*(array+great) = aux;
great--;
}
else // if the value is the same as the pivot
i++;
}
quickSort3Way(array, start, low-1); // Recursively change the pivot
quickSort3Way(array, great+1, end); // To sort the other elements
}
}