-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsort.c
161 lines (136 loc) · 3.45 KB
/
rsort.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
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
struct range {
int lo;
int hi;
};
static void _swp(void *a, void *b, size_t s)
{
unsigned char t;
size_t c = 0;
do {
t = *(unsigned char *)(a + c);
*(unsigned char *)(a + c) = *(unsigned char *)(b + c);
*(unsigned char *)(b + c) = t;
} while(++c < s);
}
int part(void *p, size_t s, int low, int high, int (*cmp)(void *, void *))
{
void *k;
int i, j;
int piv = (high + low) / 2;
_swp((p + s * piv), (p + s * (high)), s);
k = (p + s * (high));
for(i = j = low; i < high; i++) {
if(cmp((p + s * i), k) <= 0) {
_swp((p + s * i), (p + s * j), s);
j++;
}
}
_swp((p + s * j), (p + s * (high)), s);
return j;
}
void _qs(void *p, size_t s, int low, int high, int (*cmp)(void *, void *))
{
if(low < high) {
int pivot = part(p, s, low, high, cmp);
_qs(p, s, low, pivot - 1, cmp);
_qs(p, s, pivot + 1, high, cmp);
}
}
void qsrt(void *p, size_t len, size_t nelm, int (*cmp)(void *, void *))
{
if(len > 1 && p != NULL)
_qs(p, len, 0, nelm - 1, cmp);
}
/*
int _qsrt(void *r, size_t s, size_t len, int low, int high, int (*cmp)(void *, void *))
{
void *p, *t;
int i,j,k;
i = low;
j = high;
p = (r + high);
if(low < high) {
do {
while ((i < j) && (cmp(*(r + i), p) <= 0))
i++;
while ((j > i) && (cmp(*(r + j), p) >= 0))
j--;
if (i < j) {
swp(r + i, r + j, s);
}
} while (i < j);
*(r + high) = *(r + i);
*(r + i) = p;
qsortrange(r, low, i - 1);
qsortrange(r, j + 1, high);
}
}
*/
int rcmp(void *a, void *b)
{
struct range *r, *v;
r = (struct range *)a;
v = (struct range *)b;
if(r->lo < v->lo)
return -1;
else if(r->lo == v->lo) {
if(r->hi < v->hi)
return -1;
else if(r->hi == v->hi)
return 0;
else
return 1;
}
else
return 1;
}
int icmp(void *a, void *b)
{
int *i = (int *)a;
int *j = (int *)b;
if(*i < *j)
return -1;
else if(*i == *j)
return 0;
else
return 1;
}
#define NUM 60
int main(int argc, char *argv[])
{
//struct range r[] = { {4,8}, {2,6}, {31,43}, {30,111}, {322,334}, {11,121} };
struct range r[NUM] = {0};
//int ints[] = {21, 2, 11, 33, 20, 19, 10, 6, 8, 12, 29, 32, 13};
int ints[NUM];
int i;
//_swp(&ints[8], &ints[4], sizeof(int));
//qsrt(ints, sizeof(int), NUM, icmp);
srand(time(NULL) ^ getpid());
for(i = 0; i < NUM; i++) {
r[i].lo = rand() % 100;
r[i].hi = r[i].lo + rand() % 100;
printf("%p: %d-%d\n", &r[i], r[i].lo, r[i].hi);
}
//swp(&r[atoi(argv[1])], &r[atoi(argv[2])], sizeof(struct range));
qsrt(r, sizeof(struct range), NUM, rcmp);
printf("\n---------------------\n");
for(i = 0; i < NUM; i++) {
printf("%p: %d-%d\n", &r[i], r[i].lo, r[i].hi);
}
// qsrt(r, 6, sizeof(struct range), rcmp);
/*srand(time(NULL));
for(i = 0; i < 30; i++) {
r[i].lo = rand() % 100;
r[i].hi = rand() % 200;
}*/
/* for(i = 0; i < 30; i++)
printf("%d %d\n", r[i].lo, r[i].hi);
printf("*****\n");
qsortrange(r, sizeof(struct range), 0, 29, rcmp);
for(i = 0; i < 30; i++)
printf("%d %d\n", r[i].lo, r[i].hi);
*/
return 0;
}