June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,10 @@
|
|||
(defn pancake-sort
|
||||
[arr]
|
||||
(if (= 1 (count arr))
|
||||
arr
|
||||
(when-let [mx (apply max arr)]
|
||||
(let [tk (split-with #(not= mx %) arr)
|
||||
tail (second tk)
|
||||
torev (concat (first tk) (take 1 tail))
|
||||
head (reverse torev)]
|
||||
(cons mx (pancake-sort (concat (drop 1 head) (drop 1 tail))))))))
|
||||
|
|
@ -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).
|
||||
].
|
||||
|
|
@ -1,21 +1,14 @@
|
|||
function pancakesort!{T<:Real}(a::Array{T,1})
|
||||
len = length(a)
|
||||
if len < 2
|
||||
return a
|
||||
end
|
||||
function pancakesort!(arr::Vector{<:Real})
|
||||
len = length(arr)
|
||||
if len < 2 return arr end
|
||||
for i in len:-1:2
|
||||
j = indmax(a[1:i])
|
||||
i != j || continue
|
||||
a[1:j] = reverse(a[1:j])
|
||||
a[1:i] = reverse(a[1:i])
|
||||
j = indmax(arr[1:i])
|
||||
if i == j continue end
|
||||
arr[1:j] = reverse(arr[1:j])
|
||||
arr[1:i] = reverse(arr[1:i])
|
||||
end
|
||||
return a
|
||||
return arr
|
||||
end
|
||||
|
||||
pancakesort{T<:Real}(a::Array{T,1}) = pancakesort!(copy(a))
|
||||
|
||||
println("Testing a pancake sort.")
|
||||
a = [rand(-100:100) for i in 1:20]
|
||||
println("Pre: ", a)
|
||||
b = pancakesort(a)
|
||||
println("Post: ", b)
|
||||
v = rand(-10:10, 10)
|
||||
println("# unordered: $v\n -> ordered: ", pancakesort!(v))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
fn pancake_sort<T: Ord>(v: &mut [T]) {
|
||||
let len = v.len();
|
||||
// trivial case -- no flips
|
||||
if len < 2 {
|
||||
return;
|
||||
}
|
||||
for i in (0..len).rev() {
|
||||
// find index of the maximum element within `v[0..i]` (inclusive)
|
||||
let max_index = v.iter()
|
||||
.take(i + 1)
|
||||
.enumerate()
|
||||
.max_by_key(|&(_, elem)| elem)
|
||||
.map(|(idx, _)| idx)
|
||||
// safe because we already checked if `v` is empty
|
||||
.unwrap();
|
||||
// if `max_index` is not where it's supposed to be
|
||||
// do two flips to move it to `i`
|
||||
if max_index != i {
|
||||
flip(v, max_index);
|
||||
flip(v, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// function to flip a section of a mutable collection from 0..num (inclusive)
|
||||
fn flip<E: PartialOrd>(v: &mut [E], num: usize) {
|
||||
v[0..num + 1].reverse();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Sort numbers
|
||||
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
|
||||
println!("Before: {:?}", numbers);
|
||||
pancake_sort(&mut numbers);
|
||||
println!("After: {:?}", numbers);
|
||||
|
||||
// Sort strings
|
||||
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
|
||||
println!("Before: {:?}", strings);
|
||||
pancake_sort(&mut strings);
|
||||
println!("After: {:?}", strings);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue