Shell Sort for small tables. URLS to other sorts info

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
Posts: 2148
Joined: 21 Jul 2007
Has thanked: 210 times
Been thanked: 424 times

Shell Sort for small tables. URLS to other sorts info

Postby bowlesj3 » 15 Nov 2009

Here is a shell sort from for those of us who do not like the map commands. The pseudocode is an almost perfect EL code match. I tested it and it seems to work. You can cut and paste this for a clean compile and get a clean lineup of comments too.

Note: If keys are equal this sort may invert them as the link below points out. If this is important and you want to use a sort then go to the link below and find one that is marked as "stable"

for other sort algorithms see

OF NOTE: There is another type of sort called the "comb sort". I use to have the code for Cobol (Probably still do somewhere). There is lots of info on this sort on Goggle.

I had an interesting web page about quicksort, shell sort and heap sort.
However the link is wrong now so I removed it. It is unfortunate this has to happen.
If you do a google search "quicksort, shell sort and heap sort" you get a comparison.

Note: I only test for the time in this sort. If you are sorting over a day you need to adjust the test to include the date and time. Of course your sort could be completely different with different tables alltogether.

Code: Select all

{Must match tablemax}
IntraBarPersist Arw_IDs[200](0),
IntraBarPersist Arw_Date[200](0),
IntraBarPersist Arw_Time[200](0),
IntraBarPersist Arw_Dir[200](false);


{X points at last item.}
if X > 0 then {do we need to sort.}
{standard shell sort}
algorithm in pseudocode from
input: an array a of length n with array elements numbered 0 to n - 1
inc = round(n/2)
while inc > 0 do:
for i = inc .. n - 1 do:
temp = a[i]
j = i
while j >= inc and a[j - inc] > temp do:
a[j] = a[j - inc]
j = j - inc
a[j] = temp
inc = round(inc / 2.2)

{other sort algorithms -}
n = X + 1; {Add 1 to X to get N because the first item is in array[0]}
inc = round(n/2,0);
while inc > 0
for ii = inc to n - 1 {ii is used because i does not compile in EL}
tempTime = Arw_Time[ii]; {sort order Table}
tempIDs = Arw_IDs[ii]; {matching table}
tempDate = Arw_Date[ii]; {matching table}
tempDir = Arw_Dir[ii]; {matching table}
j = ii;
while j >= inc and Arw_Time[j - inc] > tempTime {sort test}
Arw_Time[j] = Arw_Time[j - inc]; {sort order Table}
Arw_IDs[j] = Arw_IDs[j - inc]; {matching table}
Arw_Date[j] = Arw_Date[j - inc]; {matching table}
Arw_Dir[j] = Arw_Dir[j - inc]; {matching table}
j = j - inc;
Arw_Time[j] = tempTime; {sort order Table}
Arw_IDs[j] = tempIDs; {matching table}
Arw_Date[j] = tempDate; {matching table}
Arw_Dir[j] = tempDir; {matching table}
end; {for ii = inc to n - 1}
inc = round(inc / 2.2,0);
end; {while inc > 0}
{End OF Shell Sort from}

Return to “User Contributed Studies and Indicator Library”