all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
class SORT{T < $IS_LT{T}} is
|
||||
|
||||
private afilter(a:ARRAY{T}, cmp:ROUT{T,T}:BOOL, p:T):ARRAY{T} is
|
||||
filtered ::= #ARRAY{T};
|
||||
loop v ::= a.elt!;
|
||||
if cmp.call(v, p) then
|
||||
filtered := filtered.append(|v|);
|
||||
end;
|
||||
end;
|
||||
return filtered;
|
||||
end;
|
||||
|
||||
private mlt(a, b:T):BOOL is return a < b; end;
|
||||
private mgt(a, b:T):BOOL is return a > b; end;
|
||||
quick_sort(inout a:ARRAY{T}) is
|
||||
if a.size < 2 then return; end;
|
||||
pivot ::= a.median;
|
||||
left:ARRAY{T} := afilter(a, bind(mlt(_,_)), pivot);
|
||||
right:ARRAY{T} := afilter(a, bind(mgt(_,_)), pivot);
|
||||
quick_sort(inout left);
|
||||
quick_sort(inout right);
|
||||
res ::= #ARRAY{T};
|
||||
res := res.append(left, |pivot|, right);
|
||||
a := res;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
class MAIN is
|
||||
main is
|
||||
a:ARRAY{INT} := |10, 9, 8, 7, 6, -10, 5, 4, 656, -11|;
|
||||
b ::= a.copy;
|
||||
SORT{INT}::quick_sort(inout a);
|
||||
#OUT + a + "\n" + b.sort + "\n";
|
||||
end;
|
||||
end;
|
||||
Loading…
Add table
Add a link
Reference in a new issue