-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpancake.jule
43 lines (38 loc) · 943 Bytes
/
pancake.jule
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
// Pancake sorts a slice using flip operations,
// where flip refers to the idea of reversing the
// slice from index `0` to `i`.
fn Pancake[T: ordered](mut arr: []T): []T {
// early return if the array too small
if len(arr) <= 1 {
ret arr
}
// start from the end of the array
mut i := len(arr) - 1
for i > 0; i-- {
// find the index of the maximum element in arr
mut max := 0
mut j := 1
for j <= i; j++ {
if arr[j] > arr[max] {
max = j
}
}
// if the maximum element is not at the end of the array
if max != i {
// flip the maximum element to the beginning of the array
arr = flip(arr, max)
// flip the maximum element to the end of the array by flipping the whole array
arr = flip(arr, i)
}
}
ret arr
}
// flip reverses the input slice from `0` to `i`.
fn flip[T: ordered](mut arr: []T, mut i: int): []T {
mut j := 0
for j < i; j++ {
arr[j], arr[i] = arr[i], arr[j]
i--
}
ret arr
}