June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,66 @@
|
|||
import extensions.
|
||||
|
||||
extension $op
|
||||
{
|
||||
pancakeSort
|
||||
[
|
||||
var list := self clone.
|
||||
|
||||
int i := list length.
|
||||
|
||||
if (i < 2) [ ^ self ].
|
||||
|
||||
while (i > 1)
|
||||
[
|
||||
int max_num_pos := 0.
|
||||
int a := 0.
|
||||
while (a < i)
|
||||
[
|
||||
if (list[a] > list[max_num_pos])
|
||||
[
|
||||
max_num_pos := a
|
||||
].
|
||||
|
||||
a += 1
|
||||
].
|
||||
|
||||
if (max_num_pos == i - 1)
|
||||
[
|
||||
];
|
||||
[
|
||||
if (max_num_pos > 0)
|
||||
[
|
||||
list flip(list length, max_num_pos + 1)
|
||||
].
|
||||
|
||||
list flip(list length, i)
|
||||
].
|
||||
i -= 1
|
||||
].
|
||||
|
||||
^ list
|
||||
]
|
||||
|
||||
flip(IntNumber length, IntNumber num)
|
||||
[
|
||||
int i := 0.
|
||||
int count := num - 1.
|
||||
while (i < count)
|
||||
[
|
||||
var swap := self[i].
|
||||
self[i] := self[count].
|
||||
self[count] := swap.
|
||||
|
||||
i += 1.
|
||||
count -= 1
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := (6, 7, 8, 9, 2, 5, 3, 4, 1).
|
||||
|
||||
console printLine("before:", list).
|
||||
console printLine("after :", list pancakeSort).
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue