Funny Programming in Delphi - Methods of Sorting

Introduction

Having very advanced means and methods for sorting unlimited amount of values, we don't think about principles of creating ordered sets and sequences. We rely on property Sorted or use full power of SQL keyword ORDER BY and aren't interested how to customize unsorted sets of number. But immersing into elementary principles of sorting can be extremely useful and funny. Let's have a look!

Methods of sorting values

We will discuss four different methods for sorting, however each of them works in a different way and it's variable regarding time demands.

  • Select sort
  • Bubble sort (simplified)
  • Select sort (classic)
  • Shake sort

Used variables

Given example assumes entering random figure of integer values into a Memo component. As soon as desired values are written, you can choose among four methods of sorting. Finally, click Go button and you'll see sorted sequence of your numbers in the second Memo component, together with time needed for main sorting procedure. According to your CPU unit speed, common count of values will consume almost no time, maybe 0.001 sec.

Let's skim over all used sorting methods:

Select sort


for I:=1 to N-1 do    //Outer loop - if minimum is found in inner loop, unsorted sequence is shortened by 1
  begin
   for J:=I+1 to N  do   //Inner loop - conducts one pass through the queue
   begin
    if A[J] < A[I]     //Each element in unsorted sequence is compared with the first one
    then begin
     POM:= A[I];
     A[I]:= A[J];
     A[J]:= POM;
    end;
   end;
  end;

Bubble sort (simplified)


for I:=N downto 2 do   //Outer loop - control variable is decreasing
  begin
   for J:=1 to I-1  do   //Inner loop - conducts one pass through the queue
   begin
    if A[J] > A[J+1]
    then
    begin
     POM:= A[J];
     A[J]:= A[J+1];
     A[J+1]:= POM;
    end;
   end;
  end;

Bubble sort (classic)


for I:=N downto 2 do    //Outer loop - control variable is decreasing
  begin
   ZM:=0;
   for J:=1 to I-1  do   //Inner loop / conducts one pass through the queue
   begin
    if A[J] > A[J+1]
    then
    begin
     POM:= A[J];
     A[J]:= A[J+1];
     A[J+1]:= POM;
     ZM:=1;       //Change is encountered
    end;
   end;
   if ZM=0 then break;  //If no change, breaks the loop
   ZM:=0;               //Elements order has changed, must be passed again
  end;

Shake sort


D:=1;                               //Lower limit is at the beginning of the queue
  H:=N-1;      //Upper limit must allow a comparison with an element one position higher
  PZM:=1;
  repeat       //Outer loop with a condition at the end
   for J:= D to H do                //Loop 1 - from bottom to top
   begin
    if A[J] > A[J+1]
    then
    begin
     POM:= A[J]; A[J]:= A[J+1]; A[J+1]:= POM;
     PZM:=J            //Last change is saved
    end;
   end;
   H:=PZM-1;  //Shifts upper limit from which searching is started
   for J:= H downto D do            //Loop 2 - from top to bottom
   begin
    if A[J+1] < A[J]
    then
    begin
     POM:= A[J]; A[J]:= A[J+1]; A[J+1]:= POM;
     PZM:=J            //Last change
    end;
   end;
   D:=PZM+1;  //Shifts lower limit from which searching is started
  until D>H;   //Are limits equal? End of outer loop
Try the fastest sorting method and let us know

If you like, you can fill first Memo component with large amount of values, for instance 10 000, and try which sorting method provides the fastest results. We'd be grateful if you give us a feedback about your experience.

Sample project regarding this topic can be downloaded from here.

About the Author

Ing. Jana Pšenčíková