Not sure of the origin of this, as I have had it around for 2 decades or
there abouts.
/*Rexx Allsorts */
Heapsort: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* Heapsort(low high)
*/
/* Heapsort sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
l=trunc(high/2)+1
r=high
do while l > low
l=l-1
call sift
end
do while r > low
temp=array.low
array.low=array.r
array.r=temp
r=r-1
call sift
end
RETURN tests switches Format(Time("Elapsed"), , 1) "Heap Sort"
sift: procedure expose array. l r tests switches sHeight num
i=l
j=2*i
temp=array.i
do while j <= r
if j < r then do
jplus1=j+1
if array.j < array.jplus1 then j=j+1
end
if temp >= array.j then leave
array.i=array.j
i=j
j=2*i
end
array.i=temp
return /* from sift */
BinInsert: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* BinInsert(low high)
*/
/* BinInsert sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
DO i = low+1 TO high
temp=array.i
l=1
r=i-1
do while l <= r
m=trunc((l+r)/2)
if temp < array.m then r=m-1
else l=m+1
end
do j=i-1 to l by -1
jplus1=j+1
array.jplus1=array.j
end
array.l=temp
end
RETURN tests switches Format(Time("Elapsed"), , 1) "Binary Insertion Sort"
StrInsert: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* StrInsert(low high)
*/
/* StrInsert sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
DO i = low+1 TO high
temp=array.i
array.0=temp
j=i-1
do while temp < array.j
jplus1=j+1
array.jplus1=array.j
j=j-1
end
jplus1=j+1
array.jplus1=temp
end
RETURN tests switches Format(Time("Elapsed"), , 1) "Straight Insertion Sort"
Shakersort: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* Shakersort(low high)
*/
/* BUBBLESORT sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
l=low+1
r=high
k=high
do until l > r
do j=r to l by -1
jminus1=j-1
if array.jminus1 > array.j then do
temp=array.jminus1
array.jminus1=array.j
array.j=temp
k=j
end
end
l=k+1
do j=l to r
jminus1=j-1
if array.jminus1 > array.j then do
temp=array.jminus1
array.jminus1=array.j
array.j=temp
k=j
end
end
r=k-1
end
RETURN tests switches Format(Time("Elapsed"), , 1) "Shaker Sort"
PrBblesort: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* PrBblesort(low high) Progressive
*/
/* BUBBLESORT sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
i=low
st1: j=i+1
if array.i > array.j THEN DO /* make an exchange
*/
temp = array.i
array.i = array.j
array.j = temp
if i >=2 then do /* can we back up and bubble? */
i=i-1
signal st1
END
END
i=i+1 /* proceed down the list */
if i < high then signal st1
RETURN tests switches Format(Time("Elapsed"), , 1) "Progressive Bubble Sort"
Bubblesort: PROCEDURE EXPOSE array. num switches sHeight tests
/*****************************************************************************/
/* BUBBLESORT(low high)
*/
/* BUBBLESORT sorts the array ARRAY. It is called with the indexes of the
*/
/* low and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
last = high - 1
DO i = low TO last
j = i + 1
IF array.i > array.j THEN DO /* make an exchange
*/
temp = array.i
array.i = array.j
array.j = temp
END
END
last = last - 1
RETURN tests switches Format(Time("Elapsed"), , 1) "Bubble Sort"
Quicksort: PROCEDURE EXPOSE array. switches sHeight tests
/*****************************************************************************/
/* QUICKSORT(low high)
*/
/* QUICKSORT sorts the array ARRAY. It is called with the indexes of the low
*/
/* and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
CALL QSort low, high
RETURN tests switches Format(Time("Elapsed"), , 1) "Quick Sort"
QSort: PROCEDURE EXPOSE array. switches sHeight tests
/*---------------------------------------------------------------------------*/
/* C. A. R. Hoare's Quicksort algorithm.
*/
/*---------------------------------------------------------------------------*/
ARG bottom, top
center = QSortIt(bottom, top)
IF center - 1 > bottom THEN CALL QSort bottom, center - 1
IF center + 1 < top THEN CALL QSort center + 1, top
RETURN
QSortIt: PROCEDURE EXPOSE array. switches sHeight tests
/*---------------------------------------------------------------------------*/
/* Heart of Quicksort.
*/
/*---------------------------------------------------------------------------*/
ARG bottom, top
choose = array.bottom
small = bottom
large = top + 1
DO WHILE (Small + 1 < large)
next = small + 1
IF array.next <= choose THEN DO
array.small = array.next
small = small + 1
array.next = choose
END
ELSE DO
large = large - 1
temp = array.large
array.large = array.next
array.next = temp
END
END
RETURN small
Shellsort: PROCEDURE EXPOSE array. switches sHeight tests
/*****************************************************************************/
/* SHELLSORT(low high)
*/
/* SHELLSORT sorts the array ARRAY. It is called with the indexes of the low
*/
/* and high bounds (defining the sub-array) to be sorted.
*/
/*****************************************************************************/
ARG low, high .
gap = low; DO WHILE (gap < high); gap = gap * 2; END /* set sublist size
*/
DO WHILE (gap > low) /* loop through the sublists, sorting them
*/
gap = gap % 2
limit = high - gap
DO scan = low TO limit /* ordinary bubble sort
*/
nextHigher = scan + gap
IF array.scan > array.nextHigher THEN DO
temp = array.nextHigher
array.nextHigher = array.scan
DO bubble = scan - gap TO low BY -gap WHILE(temp < array.bubble)
nextHigher = bubble + gap
array.nextHigher = array.bubble
END bubble
bubble = bubble + gap
array.bubble = temp
END
END scan /* end of ordinary bubble sort
*/
END
RETURN tests switches Format(Time("Elapsed"), , 1) "Shell Sort"
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus