-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsstack.c
90 lines (78 loc) · 1.19 KB
/
qsstack.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
#include <stdio.h>
#include <stdlib.h>
struct task {
int low, high;
};
struct stack {
int cap,top;
struct task *data;
};
struct stack s;
int *arr;
void push(int left, int right)
{
s.data[s.top].low=left;
s.data[s.top].high=right;
++s.top;
}
struct task pop()
{
--s.top;
return s.data[s.top];
}
int compare(int i, int j)
{
if (arr[i] > arr[j]) return 1;
else if (arr[i] < arr[j]) return -1;
else return 0;
}
void swap(int i, int j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
int partition(int low, int high)
{
int i=low, j=low;
while (j<high)
{
if(compare(j,high)<0)
{
swap(i,j);
++i;
}
++j;
}
swap(i,high);
return i;
}
int main()
{
int n,i,j;
struct task cur;
scanf("%d\n", &n);
s.data=(struct task*)malloc(n*sizeof(struct task));
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;++i)
scanf("%d", &arr[i]);
s.cap=n;
s.top=1;
s.data[0].low=0;
s.data[0].high=n-1;
do
{
cur=pop();
do
{
i=partition(cur.low,cur.high);
if (i<cur.high)
push(i+1,cur.high);
cur.high=i-1;
}while(cur.low<cur.high);
}while(s.top!=0);
for(i=0;i<n;++i)
printf("%d ", arr[i]);
printf("\n");
free(s.data);
}