-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-sort.c
74 lines (61 loc) · 1.78 KB
/
quick-sort.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
void receiveInformationInVector(int *pVector, int vectorSize);
void quickSort(int *pVector, int start, int end);
int partitions(int *pVector, int start, int end);
void printVector(int *pVector, int vectorSize);
int main(){
int vectorSize;
printf("============= Quick Sort =============\n\n");
printf("Inform the vector size: ");
scanf("%i", &vectorSize);
int vector[vectorSize];
receiveInformationInVector(vector, vectorSize);
quickSort(vector, 0, vectorSize - 1);
printVector(vector, vectorSize);
return 0;
}
void receiveInformationInVector(int *pVector, int vectorSize){
int i;
printf("\n");
for (i = 0; i < vectorSize; i++){
printf("number %i = ", i + 1);
scanf("%i", (pVector + i));
}
}
void quickSort(int *pVector, int start, int end){
int pivot;
if(end > start){
pivot = partitions(pVector, start, end);
quickSort(pVector, start, pivot - 1);
quickSort(pVector, pivot + 1, end);
}
}
int partitions(int *pVector, int start, int end){
int left, rigth, pivot, subsidiary;
left = start;
rigth = end;
pivot = *(pVector + start);
while(left < rigth){
while(*(pVector + left) <= pivot){
left++;
}
while(*(pVector + rigth) > pivot){
rigth--;
}
if(left < rigth){
subsidiary = *(pVector + left);
*(pVector + left) = *(pVector + rigth);
*(pVector + rigth) = subsidiary;
}
}
*(pVector + start) = *(pVector + rigth);
*(pVector + rigth) = pivot;
return rigth;
}
void printVector(int *pVector, int vectorSize){
int i;
printf("\nGrowing ordered vector: \n");
for (i = 0; i < vectorSize; i++){
printf("%i ", *(pVector + i));
}
}