This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsort.c
135 lines (121 loc) · 2.8 KB
/
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
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
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#define NUMMAX 100
int nums[NUMMAX];
typedef struct param {
int start;
int end;
}Param;
void show_nums(int *arr, const char *str)
{
printf("[%s]\n", str);
for(int i = 0; i < NUMMAX; i++)
{
if(i == 0)
printf("\t");
else if(i % 10 == 0)
printf("\n\t");
printf("%6d", arr[i]);
}
printf("\n");
}
void generate_nums()
{
srandom(time(NULL));
for(int i = 0; i < NUMMAX; i++)
nums[i] = rand() % 10000;
}
//快速排序
int findPos(int data[], int low, int high) {
//将大于t的元素赶到t的左边,大于t的元素赶到t的右边
int t = data[low];
while(low < high) {
while(low < high && data[high] >= t) {
high--;
}
data[low] = data[high];
while(low < high && data[low] <=t) {
low++;
}
data[high] = data[low];
}
data[low] = t;
//返回此时t在数组中的位置
return low;
}
void quickSort(int data[], int low, int high) {
if(low > high) {
return;
}
int pos = findPos(data, low, high);
quickSort(data, low, pos-1);
quickSort(data, pos+1, high);
}
//冒泡排序
void bubleSort(int data[], int n) {
int i,j,temp;
for(j=0;j<n-1;j++) {
for(i=0;i<n-j-1;i++) {
if(data[i]>data[i+1]) {
temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
}
}
}
}
int compare(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}
//使用stdlib.h里面的qsort()
void *sort(void *arg)
{
Param *param = (Param *)arg;
int left = param->start;
int right = param->end;
if(left >= right)
return NULL;
qsort(nums + left, right - left, sizeof(int), compare);
return NULL;
}
void merge(const int left, const int mid, const int right)
{
int temp[NUMMAX];
memcpy(temp, nums, NUMMAX * sizeof(int));
//show_nums(temp, "temp");
int s1 = left;
int s2 = mid + 1;
int t = left;
while(s1 <= mid && s2 <= right)
{
if(temp[s1] < temp[s2])
nums[t++] = temp[s1++];
else
nums[t++] = temp[s2++];
}
while(s1 <= mid)
nums[t++] = temp[s1++];
while(s2 <= right)
nums[t++] = temp[s2++];
}
int main()
{
generate_nums();
show_nums(nums, "unsort");
pthread_t worker_tid;
Param params[2];
params[0].start = 0;
params[0].end = NUMMAX / 2;
params[1].start = NUMMAX / 2;
params[1].end = NUMMAX;
pthread_create(&worker_tid, NULL, sort, ¶ms[1]);
//sort(¶ms[1]);
sort(¶ms[0]);
pthread_join(worker_tid, NULL);
merge(0, NUMMAX / 2 - 1, NUMMAX - 1);
show_nums(nums, "sorted");
return 0;
}