-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.c
148 lines (140 loc) · 4.7 KB
/
quicksort.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "quicksort.h"
#include <stdlib.h>
void quick_sort(data array[], int len) {
if (len > 1) {
int pivot_index = rand()%(len-1); // choose pivot
int pivot_new_index = partition(array, len, pivot_index); // partition the array and get the new pivot position
quick_sort(array, pivot_new_index); // quick sort the first part
quick_sort(array+pivot_new_index+1, len-pivot_new_index-1); // quick sort the second part
}
}
void quick_sort_debug(data array[], int len, STAT *stat) {
if (len > 1) {
int pivot_index = rand()%(len-1); // choose pivot
int pivot_new_index = partition_debug(array, len, pivot_index, stat); // partition the array and get the new pivot position
quick_sort_debug(array, pivot_new_index, stat); // quick sort the first part
quick_sort_debug(array+pivot_new_index+1, len-pivot_new_index-1, stat); // quick sort the second part
stat->recc += 2;
}
}
void quick_fast_sort(data array[], int len) {
if (len > 1) {
int pivot_index = rand()%(len-1); // choose pivot
int pivot_new_index = partition_fast(array, len, pivot_index); // partition the array and get the new pivot position
quick_fast_sort(array, pivot_new_index); // quick sort the first part
quick_fast_sort(array+pivot_new_index+1, len-pivot_new_index-1); // quick sort the second part
}
}
void quick_fast_sort_debug(data array[], int len, STAT *stat) {
if (len > 1) {
int pivot_index = rand()%(len-1); // choose pivot
int pivot_new_index = partition_fast_debug(array, len, pivot_index, stat); // partition the array and get the new pivot position
quick_fast_sort_debug(array, pivot_new_index, stat); // quick sort the first part
quick_fast_sort_debug(array+pivot_new_index+1, len-pivot_new_index-1, stat); // quick sort the second part
stat->recc += 2;
}
}
int partition(data array[], int len, int pivot_index) {
data pivot_value = array[pivot_index];
array[pivot_index] = array[len-1]; // swap pivot with last element
array[len-1] = pivot_value;
int store = 0; // storing index
int i;
for (i = 0; i < len-1; i++) // left ≤ i ≤ right-2 (pivot is at right-1)
if (array[i] < pivot_value) {
data tmp = array[i];
array[i] = array[store];
array[store] = tmp;
store++;
}
array[len-1] = array[store]; // move pivot to its final place
array[store] = pivot_value;
return store; // return the position of the pivot
}
int partition_debug(data array[], int len, int pivot_index, STAT *stat) {
data pivot_value = array[pivot_index];
data tmp;
stat->allo += 2;
stat->space = 2;
array[pivot_index] = array[len-1]; // swap pivot with last element
array[len-1] = pivot_value;
stat->swap++;
int store = 0; // storing index
int i;
for (i = 0; i < len-1; i++) { // left ≤ i ≤ right-2 (pivot is at right-1)
if (array[i] < pivot_value) {
tmp = array[i];
array[i] = array[store];
array[store] = tmp;
store++;
stat->swap++;
}
stat->comp++;
}
array[len-1] = array[store]; // move pivot to its final place
array[store] = pivot_value;
stat->swap++;
return store; // return the position of the pivot
}
int partition_fast(data array[], int len, int pivot_index) {
data pivot_value = array[pivot_index];
array[pivot_index] = array[len-1]; // swap pivot with last element
array[len-1] = pivot_value;
int left = 0; // left index
int right = len-2; // right index
while (left < right) { // ( < pivot ), pivot, ( >= pivot)
while (array[left] < pivot_value && left < right)
left++;
while (array[right] >= pivot_value && left < right)
right--;
if (left < right) {
data tmp = array[left];
array[left] = array[right];
array[right] = tmp;
left++;
right--;
}
}
if (left == right)
if (array[left] < pivot_value)
left++;
array[len-1] = array[left]; // move pivot to its final place
array[left] = pivot_value;
return left; // return the position of the pivot
}
int partition_fast_debug(data array[], int len, int pivot_index, STAT *stat) {
data pivot_value = array[pivot_index];
data tmp;
stat->allo += 2;
stat->space = 2;
array[pivot_index] = array[len-1]; // swap pivot with last element
array[len-1] = pivot_value;
stat->swap++;
int left = 0; // left index
int right = len-2; // right index
while (left < right) { // ( < pivot ), pivot, ( >= pivot)
while (array[left] < pivot_value && left < right) {
left++;
stat->comp++;
}
while (array[right] >= pivot_value && left < right) {
right--;
stat->comp++;
}
if (left < right) {
tmp = array[left];
array[left] = array[right];
array[right] = tmp;
left++;
right--;
stat->swap += 2;
}
}
if (left == right)
if (array[left] < pivot_value)
left++;
array[len-1] = array[left]; // move pivot to its final place
array[left] = pivot_value;
stat->swap++;
return left; // return the position of the pivot
}