Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import std.stdio, std.array;
|
||||
|
||||
T[] strandSort(T)(/*in*/ T[] list) pure nothrow {
|
||||
static T[] merge(T[] left, T[] right) pure nothrow {
|
||||
T[] strandSort(T)(const(T)[] list) pure nothrow {
|
||||
static T[] merge(const(T)[] left, const(T)[] right) pure nothrow {
|
||||
T[] res;
|
||||
while (!left.empty && !right.empty) {
|
||||
if (left.front <= right.front) {
|
||||
|
|
@ -19,8 +19,8 @@ T[] strandSort(T)(/*in*/ T[] list) pure nothrow {
|
|||
while (!list.empty) {
|
||||
auto sorted = list[0 .. 1];
|
||||
list.popFront;
|
||||
T[] leftover;
|
||||
foreach (item; list)
|
||||
typeof(sorted) leftover;
|
||||
foreach (const item; list)
|
||||
(sorted.back <= item ? sorted : leftover) ~= item;
|
||||
result = merge(sorted, result);
|
||||
list = leftover;
|
||||
|
|
@ -30,6 +30,6 @@ T[] strandSort(T)(/*in*/ T[] list) pure nothrow {
|
|||
}
|
||||
|
||||
void main() {
|
||||
auto arr = [-2,0,-2,5,5,3,-1,-3,5,5,0,2,-4,4,2];
|
||||
const arr = [-2, 0, -2, 5, 5, 3, -1, -3, 5, 5, 0, 2, -4, 4, 2];
|
||||
arr.strandSort.writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,32 +2,25 @@ class Array
|
|||
def strandsort
|
||||
a = self.dup
|
||||
result = []
|
||||
while a.length > 0
|
||||
until a.empty?
|
||||
sublist = [a.shift]
|
||||
a.each_with_index .
|
||||
inject([]) do |remove, (val, idx)|
|
||||
if val > sublist[-1]
|
||||
sublist << val
|
||||
remove.unshift(idx)
|
||||
end
|
||||
remove
|
||||
end .
|
||||
each {|idx| a.delete_at(idx)}
|
||||
a.each_with_index.each_with_object([]) { |(val, idx), remove|
|
||||
next if val <= sublist.last
|
||||
sublist << val
|
||||
remove << idx
|
||||
}.reverse_each {|idx| a.delete_at(idx)}
|
||||
|
||||
idx = 0
|
||||
while idx < result.length and not sublist.empty?
|
||||
if sublist[0] < result[idx]
|
||||
result.insert(idx, sublist.shift)
|
||||
end
|
||||
idx += 1
|
||||
result.each_index do |idx|
|
||||
break if sublist.empty?
|
||||
result.insert(idx, sublist.shift) if sublist[0] < result[idx]
|
||||
end
|
||||
result += sublist if not sublist.empty?
|
||||
result += sublist
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def strandsort!
|
||||
self.replace(strandsort)
|
||||
replace(strandsort)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue