-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_5.cpp
65 lines (65 loc) · 1.02 KB
/
2_5.cpp
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
#include<iostream>
using namespace std;
typedef int datatype;
const int maxsize = 5;
typedef struct{
datatype data[maxsize];
int n;
}sqlist;
void change(sqlist *l,int i ,int j){
datatype temp;
temp = l->data[i];
l->data[i] = l->data[j];
l->data[j] = temp;
}
int print(sqlist *l){
int j = l->n;
while(j-- > 0){
cout << l -> data[l -> n - j - 1];
cout << " ";
}
cout<<endl;
}
void rank(sqlist *l,int low,int high){
if(low >= high)
return;
int i = low;
int j = high;
int key = l->data[i];
while(i < j){
while(i < j && l->data[j] >= key)
j--;
change(l,i,j);
while(i < j && l->data[i] <= key)
i++;
change(l,i,j);
}
rank(l,low,i - 1);
rank(l,i + 1,high);
}
void initiate(sqlist *l){
int i = 0;
datatype data;
cin >> data;
l -> data[i] = data;
while(i < maxsize - 1){
i++;
cin >> data;
if(data == -1)
break;
else
l -> data[i] = data;
}
if(data == -1)
i--;
l -> n = i + 1;
print(l);
rank(l,0,l->n - 1);
}
int main(void){
sqlist list;
sqlist *l = &list;
initiate(l);
print(l);
return 0;
}