Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
|
|
@ -0,0 +1,67 @@
|
|||
BEGIN # circlesort #
|
||||
|
||||
# swaps a and b #
|
||||
PRIO =:= = 9;
|
||||
OP =:= = ( REF INT a, b )VOID: BEGIN INT t = a; a := b; b := t END;
|
||||
|
||||
# sorts data in-place and returns a reference to data #
|
||||
OP CIRCLESORT = ( REF[]INT data )REF[]INT:
|
||||
BEGIN
|
||||
PROC circlesort = ( REF[]INT data, INT low, high, swaps so far )INT:
|
||||
IF low >= high THEN swaps so far
|
||||
ELSE
|
||||
INT swaps := swaps so far;
|
||||
INT hi := high;
|
||||
INT lo := low;
|
||||
INT mid = ( hi - lo ) OVER 2;
|
||||
WHILE lo < hi DO
|
||||
IF data[ lo ] > data[ hi ] THEN
|
||||
data[ lo ] =:= data[ hi ];
|
||||
swaps +:= 1
|
||||
FI;
|
||||
lo +:= 1;
|
||||
hi -:= 1
|
||||
OD;
|
||||
IF lo = hi AND hi < UPB data THEN
|
||||
IF data[ lo ] > data[ hi + 1 ] THEN
|
||||
data[ lo ] =:= data[ hi + 1 ];
|
||||
swaps +:= 1
|
||||
FI
|
||||
FI;
|
||||
swaps := circlesort( data, low, low + mid, swaps );
|
||||
swaps := circlesort( data, low + mid + 1, high, swaps )
|
||||
FI # circlesort # ;
|
||||
|
||||
WHILE circlesort( data, LWB data, UPB data, 0 ) > 0 DO SKIP OD;
|
||||
data
|
||||
END # CIRCLESORT # ;
|
||||
|
||||
# prints the elements of an array of integers separated by spaces #
|
||||
OP SHOW = ( []INT list )VOID:
|
||||
FOR i FROM LWB list TO UPB list DO
|
||||
print( ( " ", whole( list[ i ], 0 ) ) )
|
||||
OD # SHOW # ;
|
||||
|
||||
# tests the CIRCLESORT operator #
|
||||
PROC test circle sort = ( []INT unsorted )VOID:
|
||||
BEGIN
|
||||
[ LWB unsorted : UPB unsorted ]INT data := unsorted;
|
||||
print( ( "[" ) );
|
||||
SHOW unsorted;
|
||||
print( ( " ]", newline, " -> [" ) );
|
||||
SHOW CIRCLESORT data;
|
||||
print( ( " ]", newline ) )
|
||||
END # test circle sort # ;
|
||||
|
||||
# task test case #
|
||||
test circle sort( ( 6, 7, 8, 9, 2, 5, 3, 4, 1 ) );
|
||||
# test cases from the Action! sample #
|
||||
test circle sort( ( 1, 4, -1, 0, 3, 7, 4, 8, 20, -6 ) );
|
||||
test circle sort( ( 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 ) );
|
||||
test circle sort( ( 101, 102, 103, 104, 105, 106, 107, 108 ) );
|
||||
test circle sort( ( 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1 ) );
|
||||
# additional tests #
|
||||
test circle sort( ( ) );
|
||||
test circle sort( ( 1 ) )
|
||||
|
||||
END
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
global d[] .
|
||||
func circsort lo hi swaps .
|
||||
if lo = hi
|
||||
return swaps
|
||||
.
|
||||
high = hi
|
||||
low = lo
|
||||
mid = (hi - lo) div 2
|
||||
while lo < hi
|
||||
if d[lo] > d[hi]
|
||||
swap d[lo] d[hi]
|
||||
swaps += 1
|
||||
.
|
||||
lo += 1
|
||||
hi -= 1
|
||||
.
|
||||
if lo = hi
|
||||
if d[lo] > d[hi + 1]
|
||||
swap d[lo] d[hi + 1]
|
||||
swaps += 1
|
||||
.
|
||||
.
|
||||
swaps = circsort low (low + mid) swaps
|
||||
swaps = circsort (low + mid + 1) high swaps
|
||||
return swaps
|
||||
.
|
||||
d[] = [ -4 -1 1 0 5 -7 -2 4 -6 -3 2 6 3 7 -5 ]
|
||||
while circsort 1 len d[] 0 > 0
|
||||
#
|
||||
.
|
||||
print d[]
|
||||
Loading…
Add table
Add a link
Reference in a new issue