-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_quick_sort_algorithm.cpp
More file actions
53 lines (44 loc) · 1.04 KB
/
_quick_sort_algorithm.cpp
File metadata and controls
53 lines (44 loc) · 1.04 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 15
#define SWAP(x,y,t) ((t) = (x), (x) = (y), (y) = (t))
void makeList(int list[]){
for(int i =0; i<SIZE; i++)
list[i] = rand() %100 +1;
}
void printList(int list[]){
for(int i=0; i<SIZE; i++)
printf("%d ", list[i]);
printf("\n");
}
int partition(int list[], int left, int right){
int pivot, temp, low, high;
pivot = list[left];
low = left;
high = right + 1;
do{
do low++;
while (list[low] <pivot );
do high--;
while (list[high] > pivot);
if(low < high) SWAP(list[low], list[high], temp);
}while(low < high);
SWAP(list[left[, list[high], temp);
return high;
}
void quickSort(int list[], int left, int right){
if(left < right){
int q = partition(list, left, right);
quickSort(list, left, q-1);
quickSort(list, q+1, right);
}
}
int main(){
int list[SIZE];
srand(time(NULL));
makeList(list);
printfList(list);
quickSort(list, 0, SIZE-1);
printList(list);
}