-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsorting.c
More file actions
214 lines (196 loc) · 4.41 KB
/
sorting.c
File metadata and controls
214 lines (196 loc) · 4.41 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
#include <time.h>
#define DEBUG 0
#define DEBUG2 0
int quicksort1(int *x, int left, int right);
int quicksort2(int *x, int left, int right);
int main()
{
clock_t t1, t2; // variables for computing clocks
int *x, *y, s, p;
double T1;
int i, j, N;
srand( time(NULL) );
for(N=160000;N<=40960000;N*=2)
{
x = (int *) malloc( N * sizeof(int) );
y = (int *) malloc( N * sizeof(int) );
//#pragma omp parallel
{
//#pragma omp parallel for
for(i=0;i<N;++i)
{
y[i] = x[i] = rand() % N;
}
}
#if DEBUG // if DEBUG == 1, then compile the following codes
for(i=0;i<N;++i)
{
printf("x[%d]=%d\n",i,x[i]);
}
#endif // end of if block
#if DEBUG
t1 = clock();
for(i=0;i<N;++i)
{
for(j=i+1;j<N;++j)
{
if(y[i]<y[j])
{
s = y[i];
y[i] = y[j];
y[j] = s;
}
}
}
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("Sorting %d elements: %f\n",N, T1);
#endif
for(i=0;i<N;++i) y[i] = x[i];
t1 = clock();
quicksort1(y,0,N);
#if DEBUG
for(i=0;i<N;++i)
{
printf("y[%d]=%d\n",i,y[i]);
}
#endif
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(1)Quick Sorting %d elements: %f\n",N, T1);
for(i=0;i<N;++i) y[i] = x[i];
t1 = clock();
quicksort2(y,0,N);
#if DEBUG
for(i=0;i<N;++i)
{
printf("y[%d]=%d\n",i,y[i]);
}
#endif
t2 = clock();
T1 = (t2-t1)/(double) CLOCKS_PER_SEC;
printf("(2)Quick Sorting %d elements: %f\n",N, T1);
free(x);
free(y);
}
return 0;
}
int quicksort1(int *x, int left, int right)
{
int i, j, k, pivot, pivot_loc, N = right-left;
int *y;
if(left < right-1)
{
// 先找一個 pivot, 然後把比 pivot 小的 放到前面, 比pivot 大的放到後面
// 想辦法做
// 再去排 pivot 的左右兩邊
// quicksort(x, left, pivot_location);
// quicksort(x, pivot_location+1, right);
y = (int *) malloc(N*sizeof(int));
pivot_loc = left+(rand() % N);
pivot = x[pivot_loc];
x[pivot_loc] = x[left];
x[left] = pivot;
i = 0; j = N-1;
// x: 5(pivot) 4 4 3 2 7 8 9 10 -> y: 4 4 3 2 (i=j=4) 10 9 8 7
// x: 4(pivot) 4 4 3 2 7 8 9 10 -> "<" y: 3 2 (i=j=2) 10 9 8 7 4 4
// x: 4(pivot) 4 4 3 2 7 8 9 10 -> "<=" y: 4 4 3 2 (i=j=4) 10 9 8 7
// x: 5 4 4 3 2 7 8 9 10 , pivot_loc = 5, pivot = 7
// x: 7 4 4 3 2 5 8 9 10 -> y:
for(k=1;k<N;++k)
{
if(x[left+k] <= pivot)
{
y[i++] = x[left+k]; // i = i + 1;
//i = i + 1; // y[i] = x[left+k]; --> y[++i] = x[left+k];
}
else
{
y[j--] = x[left+k];
// j = j - 1;
}
}
y[i] = pivot;
#if DEBUG2
printf("%d %d %d %d %d\n",left,i,j,pivot,N);
for(k=0;k<N;++k)
{
printf("y[%d]=%d\n",k,y[k]);
}
system("pause");
#endif
for(k=0;k<N;++k)
{
x[left+k] = y[k];
}
free(y);
quicksort1(x,left,left+i);
quicksort1(x,left+i+1,right);
}
else
{
return 1;
}
}
int quicksort2(int *x, int left, int right)
{
int i, j, k;
int pivot, t;
//
if(left < right-1)
{
pivot = x[left];
i = left+1;
j = right-1;
//
while(1)
{ // x: 5(pivot) 4 8 3 2 7 3 2 10 -> i = 2, j = 7 (交換 x[i], x[j])
// 5(pivot) 4 2 3 2 7 3 8 10 -> i = 5, j = 6 (交換 x[i], x[j])
// 5(pivot) 4 2 3 2 3 7 8 10 -> i = 6, j = 5 (不交換了!!!)
// 3 5 (location: j)
// x: 4(pivot) 4 8 3 2 7 3 4 10 -> i = 2, j = 7 (交換 x[i], x[j])
// 4(pivot) 4 4 3 2 7 3 8 10 -> i = 5, j = 6 (交換 x[i], x[j])
// 4(pivot) 4 4 3 2 3 7 8 10 -> i = 6, j = 5 (不交換了!!!)
// 3 4 (location: j)
// x: 8 2 1 8 7 8 9 4 5 8 i = 3, j = 9 (交換 x[i], x[j])
// x: 8 2 1 8 7 8 9 4 5 8
while(i < right && pivot >= x[i]) i++; // 往右邊找到第一個 pivot < x[i]
while(j > left && pivot < x[j]) j--; // 往左邊找到第一個 pivot >= x[j]
#if DEBUG2
printf("%d %d %d\n", i,j,pivot);
#endif
if(i>=j) break;
t = x[i];
x[i] = x[j];
x[j] = t;
#if DEBUG2
for(k=left;k<right;++k)
{
printf("x[%d]=%d\n",k,x[k]);
}
system("pause");
#endif
}
//t = x[left];
x[left] = x[j];
x[j] = pivot;
#if DEBUG2
printf("i=%d,j=%d\n",i,j);
for(k=left;k<right;++k)
{
printf("x[%d]=%d\n",k,x[k]);
}
system("pause");
#endif
quicksort2(x, left, j);
quicksort2(x, j+1, right);
}
else
{
return 1;
}
}