-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsort.sg
More file actions
157 lines (134 loc) · 2.91 KB
/
sort.sg
File metadata and controls
157 lines (134 loc) · 2.91 KB
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
proc bitxor(a: Int, b: Int) -> Int = core {
bitwise-xor [SP], [SP - 1]
pop
} in
proc bitor(a: Int, b: Int) -> Int = core {
bitwise-or [SP], [SP - 1]
pop
} in
proc bitnand(a: Int, b: Int) -> Int = core {
bitwise-nand [SP], [SP - 1]
pop
} in
proc bitand(a: Int, b: Int) -> Int = core {
bitwise-and [SP], [SP - 1]
pop
} in
proc bitnot(x: Int) -> Int = core {
bitwise-not [SP]
} in
proc alloc(size: Int) -> &Cell = std {
alloc [SP]
} in
proc free(ptr: &Int) = std {
free [SP]
pop
} in
proc puthex(n: Int, lower: Bool) -> None = {
if (n < 16) {
put (n + if (n < 10) {
48
} else if lower {
87
} else {
55
}) as Char
} else {
puthex(n / 16, lower);
puthex(n % 16, lower)
}
} in
proc putbyte(n: Int, lower: Bool) -> None = {
puthex(n / 16, lower);
puthex(n % 16, lower)
} in
proc ln() -> None = {
put '\n'
} in
proc min(x: Int, y: Int) -> Int = {
if (x < y) x else y
} in
proc max(x: Int, y: Int) -> Int = {
if (x > y) x else y
} in
type List = struct { data: Int, next: &List } in
proc sort(node: &List) -> None = {
if (node->next != Null) {
sort(node->next);
let a = min(node->data, node->next->data),
b = max(node->data, node->next->data)
in {
if (b != node->data) {
sort(node->next);
};
node->data = a;
node->next->data = b;
};
}
} in
proc len(node: &List) -> Int = {
if (node != Null) {
len(node->next) + 1
} else {
0
}
} in
proc index(node: &List, n: Int) -> &List = {
while n {
node = node->next;
n -= 1;
};
node
} in
proc swapi(a: &Int, b: &Int) = {
let tmp = (*a) in {
(*a) = *b;
(*b) = tmp;
}
} in
proc partition_arr(arr: &Int, low: Int, high: Int) -> Int = {
let pivot = arr[high],
i = low - 1,
j = low in {
while j < high {
if (arr[j] <= pivot) {
i += 1;
swapi(&arr[j], &arr[i]);
};
j += 1
};
swapi(&arr[i + 1], &arr[high]);
i + 1
}
} in
proc quicksort_arr(arr: &Int, low: Int, high: Int) = {
if (low < high) {
let pi = partition_arr(arr, low, high) in {
quicksort_arr(arr, low, pi - 1);
quicksort_arr(arr, pi + 1, high);
}
}
} in
proc get_int() -> Int = core {
next SP
get [SP], stdin.int
} in
proc get_list(size: Int) -> &Int = {
let arr: &Int = alloc(size) as &Int, i = 0 in {
while (i < size) {
arr[i] = get_int();
i += 1;
};
arr
}
} in {
let len = get_int(),
list = get_list(len),
i = 0 in {
quicksort_arr(list, 0, len - 1);
while i < len {
put list[i];
i += 1;
};
}
}