Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,164 @@
* quicksort 14/09/2015
QUICKSOR CSECT
USING QUICKSOR,R15 set base register
BEGIN MVC A,=F'1' a(1)=1
MVC B,=A((A-T)/4) b(1)=hbound(t)
L R6,=F'1' k=1
WHILEK LTR R6,R6 do while k^=0
BZ EWHILEK
LR R1,R6 k
SLA R1,2 ~
L R10,A-4(R1) l=a(k)
LR R1,R6 k
SLA R1,2 ~
L R11,B-4(R1) m=b(k)
BCTR R6,0 k=k-1
LR R4,R11 m
C R4,=F'2' if m<2
BL WHILEK then iterate
LR R2,R10 l
AR R2,R11 +m
BCTR R2,0 -1
ST R2,X x=l+m-1
LR R2,R11 m
SRA R2,1 m/2
AR R2,R10 +l
ST R2,Y y=l+m/2
L R1,X x
SLA R1,2 ~
L R4,T-4(R1) r4=t(x)
L R1,Y y
SLA R1,2 ~
L R5,T-4(R1) r5=t(y)
LR R1,R10 l
SLA R1,2 ~
L R3,T-4(R1) r3=t(l)
IF CR R4,R3 if t(x)<t(l)
BNL ELSE
CR R5,R4 if t(y)<t(x)
BNL IFX
LR R7,R4 p=t(x)
L R1,X x
SLA R1,2 ~
ST R3,T-4(R1) t(x)=t(l)
B EIFX
IFX CR R5,R3 if t(y)>t(l)
BNH IFXELIF
LR R7,R3 p=t(l)
B EIFX
IFXELIF LR R7,R5 p=t(y)
L R1,Y y
SLA R1,2 ~
ST R3,T-4(R1) t(y)=t(l)
EIFX B ENDIF
ELSE CR R5,R3 if t(y)<t(l)
BNL IFY
LR R7,R3 p=t(l)
B ENDIF
IFY CR R5,R4 if t(y)>t(x)
BNH IFYELIF
LR R7,R4 p=t(x)
L R1,X x
SLA R1,2 ~
ST R3,T-4(R1) t(x)=t(l)
B ENDIF
IFYELIF LR R7,R5 p=t(y)
L R1,Y y
SLA R1,2 ~
ST R3,T-4(R1) t(y)=t(l)
ENDIF LA R8,1(R10) i=l+1
L R9,X j=x
FOREVER EQU *
LOOPWI CR R8,R9 i<=j
BH ELOOPWI
LR R1,R8 i
SLA R1,2 ~
L R2,T-4(R1) t(i)
CR R2,R7 t(i)<=p
BH ELOOPWI
LA R8,1(R8) i=i+1
B LOOPWI
ELOOPWI EQU *
LOOPWJ CR R8,R9 i<j
BNL ELOOPWJ
LR R1,R9 j
SLA R1,2 ~
L R2,T-4(R1) t(j)
CR R2,R7 t(j)>=p
BL ELOOPWJ
BCTR R9,0 j=j-1
B LOOPWJ
ELOOPWJ CR R8,R9 if i>=j
BNL EFOREVER then leave segment finished
LR R1,R8 i
SLA R1,2 ~
LA R2,T-4(R1) @t(i)
LR R1,R9 j
SLA R1,2 ~
LA R3,T-4(R1) @t(j)
L R0,0(R2) w=t(i)
MVC 0(4,R2),0(R3) t(i)=t(j) swap t(i),t(j)
ST R0,0(R3) t(j)=w
B FOREVER
EFOREVER LR R9,R8 j=i
BCTR R9,0 j=i-1
LR R1,R9 j
SLA R1,2 ~
LA R3,T-4(R1) @t(j)
L R2,0(R3) t(j)
LR R1,R10 l
SLA R1,2 ~
ST R2,T-4(R1) t(l)=t(j)
ST R7,0(R3) t(j)=p
LA R6,1(R6) k=k+1
LR R1,R6 k
SLA R1,2 ~
LA R4,A-4(R1) r4=@a(k)
LA R5,B-4(R1) r5=@b(k)
C R8,Y if i<=y
BH IFIHY
ST R8,0(R4) a(k)=i
L R2,X x
SR R2,R8 -i
LA R2,1(R2) +1
ST R2,0(R5) b(k)=x-i+1
LA R6,1(R6) k=k+1
ST R10,4(R4) a(k)=l
LR R2,R9 j
SR R2,R10 -l
ST R2,4(R5) b(k)=j-l
B EIFIHY
IFIHY ST R10,4(R4) a(k)=l
LR R2,R9 j
SR R2,R10 -l
ST R2,0(R5) b(k)=j-l
LA R6,1(R6) k=k+1
ST R8,4(R4) a(k)=i
L R2,X x
SR R2,R8 -i
LA R2,1(R2) +1
ST R2,4(R5) b(k)=x-i+1
EIFIHY B WHILEK
EWHILEK LA R3,PG ibuffer
LA R4,T @t(i)
LOOPI C R4,=A(A) do i=1 to hbound(t)
BH ELOOPI
L R2,0(R4) t(i)
XDECO R2,XD edit t(i)
MVC 0(4,R3),XD+8 put in buffer
LA R3,4(R3) ibuffer=ibuffer+1
LA R4,4(R4) i=i+1
B LOOPI
ELOOPI XPRNT PG,80 print bufffer
RETURN XR R15,R15 set return code
BR R14 return to caller
T DC F'10',F'9',F'9',F'6',F'7',F'16',F'1',F'16',F'17',F'15'
DC F'1',F'9',F'18',F'16',F'8',F'20',F'18',F'2',F'19',F'8'
A DS ((A-T)/4)F same size as T
B DS ((A-T)/4)F same size as T
X DS F
Y DS F
PG DS CL80
XD DS CL12
YREGS
END QUICKSOR

View file

@ -1,47 +1,62 @@
PROC partition =(REF [] DATA array, PROC (REF DATA, REF DATA) BOOL cmp)INT: (
INT begin:=LWB array;
INT end:=UPB array;
WHILE begin < end DO
WHILE begin < end DO
IF cmp(array[begin], array[end]) THEN
DATA tmp=array[begin];
array[begin]:=array[end];
array[end]:=tmp;
GO TO break while decr end
FI;
end -:= 1
OD;
break while decr end: SKIP;
WHILE begin < end DO
IF cmp(array[begin], array[end]) THEN
DATA tmp=array[begin];
array[begin]:=array[end];
array[end]:=tmp;
GO TO break while incr begin
FI;
begin +:= 1
OD;
break while incr begin: SKIP
OD;
begin
#--- Swap function ---#
PROC swap = (REF []INT array, INT first, INT second) VOID:
(
INT temp := array[first];
array[first] := array[second];
array[second]:= temp
);
PROC qsort=(REF [] DATA array, PROC (REF DATA, REF DATA) BOOL cmp)VOID: (
IF LWB array < UPB array THEN
INT i := partition(array, cmp);
PAR ( # remove PAR for single threaded sort #
qsort(array[:i-1], cmp),
qsort(array[i+1:], cmp)
)
#--- Quick sort 3 arg function ---#
PROC quick = (REF [] INT array, INT first, INT last) VOID:
(
INT smaller := first + 1,
larger := last,
pivot := array[first];
WHILE smaller <= larger DO
WHILE array[smaller] < pivot AND smaller < last DO
smaller +:= 1
OD;
WHILE array[larger] > pivot AND larger > first DO
larger -:= 1
OD;
IF smaller < larger THEN
swap(array, smaller, larger);
smaller +:= 1;
larger -:= 1
ELSE
smaller +:= 1
FI
OD;
swap(array, first, larger);
IF first < larger-1 THEN
quick(array, first, larger-1)
FI;
IF last > larger +1 THEN
quick(array, larger+1, last)
FI
);
#--- Quick sort 1 arg function ---#
PROC quicksort = (REF []INT array) VOID:
(
IF UPB array > 1 THEN
quick(array, 1, UPB array)
FI
);
MODE DATA = INT;
PROC cmp=(REF DATA a,b)BOOL: a>b;
#***************************************************************#
main:
(
[10]INT a;
FOR i FROM 1 TO UPB a DO
a[i] := ROUND(random*1000)
OD;
main:(
[]DATA const l=(5,4,3,2,1);
[UPB const l]DATA l:=const l;
qsort(l,cmp);
printf(($g(3)$,l))
print(("Before:", a));
quicksort(a);
print((newline, newline));
print(("After: ", a))
)

View file

@ -33,10 +33,11 @@ begin
end loop;
exit when Left >= Right;
Swap(Item(Left), Item(Right));
if Pivot_Index = Left then
Pivot_Index := Right;
elsif Pivot_Index = Right then
Pivot_Index := Left;
if Left < Item'Last then
Left := Index_Type'Succ(Left);
end if;
if Right > Item'First then
Right := Index_Type'Pred(Right);
end if;
end loop;
if Right > Item'First then

View file

@ -20,7 +20,7 @@ void quick_sort (int *a, int n) {
quick_sort(a + i, n - i);
}
int main () {
int main (void) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;

View file

@ -0,0 +1,9 @@
defmodule QuickSort do
def qsort([]) do
[]
end
def qsort([pivot | rest]) do
{ left, right } = Enum.partition(rest, fn(x) -> x < pivot end)
qsort(left) ++ [pivot] ++ qsort(right)
end
end

View file

@ -1,15 +1,18 @@
# (sort keep compare xs) sorts the list xs using the three-way comparison
# function. It keeps duplicates if the keep flag is true, otherwise it
# discards them and returns only the unique entries.
\sort ==
(\keep\compare\xs
xs end \x\xs
\lo = (filter (\y compare y x T F F) xs)
\hi = (filter (\y compare y x F keep T) xs)
append (sort keep compare lo);
item x;
sort keep compare hi
# (sort xs) is the ordered list of all elements in list xs.
# This version preserves duplicates.
\sort==
(\xs
xs [] \x\xs
append (sort; filter (gt x) xs); # all the items less than x
cons x; append (filter (eq x) xs); # all the items equal to x
sort; filter (lt x) xs # all the items greater than x
)
# (unique xs) is the ordered list of unique elements in list xs.
\unique==
(\xs
xs [] \x\xs
append (unique; filter (gt x) xs); # all the items less than x
cons x; # x itself
unique; filter (lt x) xs # all the items greater than x
)

View file

@ -1,22 +1,31 @@
function sort(array, less) {
function swap(i, j) { var t=array[i]; array[i]=array[j]; array[j]=t }
function swap(i, j) {
var t = array[i];
array[i] = array[j];
array[j] = t;
}
function quicksort(left, right) {
if (left < right) {
var pivot = array[(left + right) >> 1];
var left_new = left, right_new = right;
var pivot = array[(left + right) / 1],
left_new = left,
right_new = right;
do {
while (less(array[left_new], pivot)
left_new++;
while (less(pivot, array[right_new])
right_new--;
if (left_new <= right_new)
swap(left_new++, right_new--);
} while (left_new <= right_new);
while (less(array[left_new], pivot) {
left_new += 1;
}
while (less(pivot, array[right_new]) {
right_new -= 1;
}
if (left_new <= right_new) {
swap(left_new, right_new);
left_new += 1;
right_new -= 1;
}
} while (left_new <= right_new);
quicksort(left, right_new);
quicksort(left_new, right);
@ -24,7 +33,7 @@ function sort(array, less) {
}
}
quicksort(0, array.length-1);
quicksort(0, array.length - 1);
return array;
}

View file

@ -1,11 +1,10 @@
Array.prototype.quick_sort = function ()
{
if (this.length <= 1)
return this;
Array.prototype.quick_sort = function () {
if (this.length < 2) { return this; }
var pivot = this[Math.round(this.length / 2)];
return this.filter(function (x) { return x < pivot }).quick_sort().concat(
this.filter(function (x) { return x == pivot })).concat(
this.filter(function (x) { return x > pivot }).quick_sort());
}
return this.filter(x => x < pivot)
.quick_sort()
.concat(this.filter(x => x == pivot))
.concat(this.filter(x => x > pivot).quick_sort());
};

View file

@ -0,0 +1,4 @@
QuickSort[{}] := {}
QuickSort[list: {__}] := With[{pivot=RandomChoice[list]},
Join[ <|1->{}, -1->{}|>, GroupBy[list,Order[#,pivot]&] ] // Catenate[ {QuickSort@#[1], #[0], QuickSort@#[-1]} ]&
]

View file

@ -4,9 +4,9 @@
# Otherwise, extract first item as pivot...
multi quicksort([$pivot, *@rest]) {
# Partition.
my @before := @rest.grep(* before $pivot);
my @after := @rest.grep(* !before $pivot);
my $before := @rest.grep(* before $pivot);
my $after := @rest.grep(* !before $pivot);
# Sort the partitions.
(quicksort(@before), $pivot, quicksort(@after))
flat quicksort($before), $pivot, quicksort($after)
}

View file

@ -1,8 +1,7 @@
sub quick_sort {
my @a = @_;
return @a if @a < 2;
my $p = splice @a, int rand @a, 1;
quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ >= $p, @a);
return @_ if @_ < 2;
my $p = splice @_, int rand @_, 1;
quick_sort(grep $_ < $p, @_), $p, quick_sort(grep $_ >= $p, @_);
}
my @a = (4, 65, 2, -31, 0, 99, 83, 782, 1);

View file

@ -0,0 +1,15 @@
function quicksort($array) {
$less, $equal, $greater = @(), @(), @()
if( $array.Count -gt 1 ) {
$pivot = $array[0]
foreach( $x in $array) {
if($x -lt $pivot) { $less += @($x) }
elseif ($x -eq $pivot) { $equal += @($x)}
else { $greater += @($x) }
}
$array = (@(quicksort $less) + @($equal) + @(quicksort $greater))
}
$array
}
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
"$(quicksort $array)"

View file

@ -1,9 +1,8 @@
class Array
def quick_sort
return self if length <= 1
pivot = sample
find_all { |i| i < pivot }.quick_sort +
find_all { |i| i == pivot } +
find_all { |i| i > pivot }.quick_sort
pivot = self[0]
less, greatereq = self[1..-1].partition { |x| x < pivot }
less.quick_sort + [pivot] + greatereq.quick_sort
end
end

View file

@ -1,8 +1,9 @@
class Array
def quick_sort
return self if length <= 1
pivot = self[0]
less, greatereq = self[1..-1].partition { |x| x < pivot }
less.quick_sort + [pivot] + greatereq.quick_sort
pivot = sample
group = group_by{ |x| x <=> pivot }
group.default = []
group[-1].quick_sort + group[0] + group[1].quick_sort
end
end

View file

@ -1,9 +1,6 @@
class Array
def quick_sort
return self if length <= 1
pivot = sample
group = group_by{ |x| x <=> pivot }
group.default = []
group[-1].quick_sort + group[0] + group[1].quick_sort
h, *t = self
h ? t.partition { |e| e < h }.inject { |l, r| l.quick_sort + [h] + r.quick_sort } : []
end
end

View file

@ -1,32 +1,58 @@
// Type alias for function that returns true if arguments are in the correct order
type OrderFunc<T> = Fn(&T, &T) -> bool;
fn main() {
// Sort numbers
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {:?}", numbers);
quick_sort(&mut numbers, &f);
println!("After: {:?}", numbers);
// Sort strings
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
println!("Before: {:?}", strings);
quick_sort(&mut strings, &f);
println!("After: {:?}", strings);
}
// Example OrderFunc which is used to order items from least to greatest
#[inline]
fn f<T: Ord>(x: &T, y: &T) -> bool {
x < y
}
// We use in place quick sort
// For details see http://en.wikipedia.org/wiki/Quicksort#In-place_version
fn quick_sort<T: Ord>(v: &mut[T]) {
fn quick_sort<T>(v: &mut [T], f: &OrderFunc<T>) {
let len = v.len();
if len < 2 {
return;
}
let pivot_index = partition(v);
let pivot_index = partition(v, f);
// Sort the left side
quick_sort(v.mut_slice(0, pivot_index));
quick_sort(&mut v[0..pivot_index], f);
// Sort the right side
quick_sort(v.mut_slice(pivot_index + 1, len));
quick_sort(&mut v[pivot_index + 1..len], f);
}
// Reorders the slice with values lower than the pivot at the left side,
// and values bigger than it at the right side.
// Also returns the store index.
fn partition<T: Ord>(v: &mut [T]) -> uint {
fn partition<T>(v: &mut [T], f: &OrderFunc<T>) -> usize {
let len = v.len();
let pivot_index = len / 2;
v.swap(pivot_index, len - 1);
let mut store_index = 0;
for i in range(0, len - 1) {
if v[i] <= v[len - 1] {
for i in 0..len - 1 {
if f(&v[i], &v[len - 1]) {
v.swap(i, store_index);
store_index += 1;
}
@ -35,19 +61,3 @@ fn partition<T: Ord>(v: &mut [T]) -> uint {
v.swap(store_index, len - 1);
store_index
}
fn main() {
// Sort numbers
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
println!("Before: {}", numbers.as_slice());
quick_sort(numbers);
println!("After: {}", numbers.as_slice());
// Sort strings
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
println!("Before: {}", strings.as_slice());
quick_sort(strings);
println!("After: {}", strings.as_slice());
}

View file

@ -0,0 +1,31 @@
Function quicksort(arr,s,n)
l = s
r = s + n - 1
p = arr(Int((l + r)/2))
Do Until l > r
Do While arr(l) < p
l = l + 1
Loop
Do While arr(r) > p
r = r -1
Loop
If l <= r Then
tmp = arr(l)
arr(l) = arr(r)
arr(r) = tmp
l = l + 1
r = r - 1
End If
Loop
If s < r Then
Call quicksort(arr,s,r-s+1)
End If
If l < t Then
Call quicksort(arr,l,t-l+1)
End If
quicksort = arr
End Function
myarray=Array(9,8,7,6,5,5,4,3,2,1,0,-1)
m = quicksort(myarray,0,12)
WScript.Echo Join(m,",")