-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort3ways.c
90 lines (84 loc) · 2.27 KB
/
quicksort3ways.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define NUMBER 100000000
void swap(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition_2w(int arr[], long int low, long int high){
int pivot = arr[high];
int i = low - 1;
for(long int j = low; j < high; j++){
if(arr[j] < pivot){
++i;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort_2w(int arr[], long int low, long int high){
if(low < high){
int pi = partition_2w(arr, low, high);
quickSort_2w(arr, low, pi - 1);
quickSort_2w(arr, pi + 1, high);
}
}
// void printArray(int arr[], long int n){
// for(long int i = 0; i < n; i++){
// printf("%ld ", arr[i]);
// }
// }
void exch(int a[], int i, int j){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void sort(int a[], int l, int r) {
if (r <= l) return;
int i = l-1, j = r;
int p = l-1, q = r;
while(1){
while (a[++i] < a[r]);
while (a[r] < a[--j]) if (j == l) break;
if (i >= j) break;
exch(a, i, j);
if (a[i]==a[r]) exch(a, ++p, i);
if (a[j]==a[r]) exch(a, --q, j);
}
exch(a, i, r);
j = i - 1;
i = i + 1;
for (int k = l ; k <= p; k++) exch(a, k, j--);
for (int k = r-1; k >= q; k--) exch(a, k, i++);
sort(a, l, j);
sort(a, i, r);
}
int cmpInt(const void *a, const void *b){
return (*(int*)a) - *((int *)b);
}
int main(){
srand((int)time(0));
clock_t start, end;
int *arr1 = (int*)malloc(NUMBER*sizeof(int));
int *arr2 = (int*)malloc(NUMBER*sizeof(int));
for(long int i = 0; i < NUMBER; ++i){
arr1[i] = 1 + rand() % 10;
}
memcpy(arr2,arr1,NUMBER);
start = clock();
sort(arr1, 0, NUMBER);
end = clock();
// printArray(arr1, NUMBER);
printf("\n");
printf("Soring time of 3 ways QS: %.2f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
time_t start2, end2;
start2 = clock();
qsort(arr2,NUMBER,sizeof(int),cmpInt);
end2 = clock();
printf("\n");
printf("Soring time of qsort: %.2f seconds\n", (double)(end2-start2)/CLOCKS_PER_SEC);
}