-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastsorteasy.c
73 lines (54 loc) · 1001 Bytes
/
fastsorteasy.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
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 20
typedef int ElemType;
typedef struct RedType
{
ElemType key;
};
typedef struct SqList
{
RedType r[MaxSize + 1];
} SqList;
int sort(SqList *s, int low, int hight)
{
int a = s->r[low].key;
int pivotkey = s->r[low].key;
while (low <hight)
{
while (low<hight&&s->r[hight].key >= pivotkey)
--hight;
s->r[low] = s->r[hight];
while (low<hight&& s->r[low].key <= pivotkey)
++low;
s->r[hight] = s->r[low];
}
s->r[low].key =a;
return low;
}
void Qsort(SqList *s, int low, int hight)
{
if (low < hight)
{
int pivotkey = sort(s, low, hight);
Qsort(s, low, pivotkey-1);
Qsort(s, pivotkey + 1, hight);
}
}
void main()
{
SqList *s;
s = (SqList *)malloc(sizeof(SqList));
int a[] = { 49, 38, 65, 97, 76, 13, 27 };
for (int i = 0; i < 7; i++)
{
s->r[i].key = a[i];
}
// int pivotkey=sort(s, 0, 7);
Qsort(s, 0, 6);
for (int i = 0; i < 7; i++)
{
printf("%d\n", s->r[i].key);
}
system("pause");
}