Sorting Viewer

A Study in Visual Basic®

by Rick Meyer       Home


Running the Viewer
Method Explanations



Other Sorting Pages
Sorting Timer
Simple Ascend Sorting
Simple Descend Sorting
Simple Bi-Direction Sorting

Note: You can skip the following instructions and Download the Project (6K).
If you are interested in code without the video calls click the Sorting Timer link above.
   To Build the Project

1. Start a new standard exe.
2. On Form1 put a Shape Control named Shape1.
3. Set the Shape1.Index property to 0.
4. On Form1 put a CommandButton named Command1.
5. Set the Command1.Index property to 0.
6. On Form1 put a Label named Label1.
7. On Form1 put a Horizontal Scrollbar named HScroll1.

   There is no need to position or size the above controls
   on the form since that is all done in the Form_Load.

Now you are ready for the code. Select all of the following code (by clicking on the word 'Option' three times) and copy it to the clipboard [Ctrl][Insert]. Then paste it into the code window of Form1 with [Shift][Insert]. Variable Key
% As Integer
& As Long
! As Single
# As Double
$ As String
Option Explicit

Const BARS% = 16        'Number of elements to sort
Const MINSPEED% = 10000
Const BARSM1% = BARS - 1
Const MAXFUNC% = 9
Const UNIT! = 255, MARGIN! = 100
Const SLOC! = UNIT * BARSM1 + MARGIN
Const TITLEBAR! = 295, BOARDERS! = 120
Const GREEN& = &H80FF80, BLUE& = &HFF8080
Const ORANGE& = &H80C0FF, RED& = &HFF
Const IHI = 32767, MIHI = -32768

Private Type atemp
    Top As Integer
    Height As Integer
End Type

Dim Atmp As atemp
Dim Spd%
Dim Ascend As Boolean, Sorting As Boolean
Dim Stepping As Boolean, Stepit As Boolean
Dim Sorts$(MAXFUNC)

Private Sub Bubble_Sort(ByVal lower%, ByVal upper%)
    Caption = "Bubble Sort"
    Dim j%, Done As Boolean
    
    Do Until Done
        Done = True
        upper = upper - 1
        For j = lower To upper
            If Compar(j, j + 1) Then
                Swap j, j + 1
                Done = False
            End If
            If Not Sorting Then Exit For
        Next
        If Not Sorting Then Exit Do
    Loop
End Sub

Private Sub BiBubble_Sort(ByVal lower%, ByVal upper%)
    Caption = "Bi-Bubble Sort"
    Dim j%, Done As Boolean
    
    Do Until Done
        Done = True
        upper = upper - 1
        For j = lower To upper
            If Compar(j, j + 1) Then
                Swap j, j + 1
                Done = False
            End If
            If Not Sorting Then Exit For
        Next
        
        If Not Sorting Or Done Then Exit Do
        
        lower = lower + 1
        For j = upper To lower Step -1
            If Compar(j - 1, j) Then
                Swap j - 1, j
                Done = False
            End If
            If Not Sorting Then Exit For
        Next
        If Not Sorting Or Done Then Exit Do
    Loop
End Sub

Private Sub Insertion_Sort(ByVal lower%, ByVal upper%)
    Caption = "Insertion Sort"
    Dim j%, low%, high%
    
    For j = lower + 1 To upper
        high = j
        Do
            low = high - 1
            
            If Compar(low, high) Then
                Swap low, high
                If Not Sorting Then Exit Do
                high = low
            Else
                Exit Do
            End If
        Loop Until high <= lower
        If Not Sorting Then Exit For
    Next
End Sub

Private Sub Shell_Sort(ByVal lower%, ByVal upper%)
    Caption = "Shell Sort"
    Dim diff%, low%, high%
    
    diff = Int(CSng(upper - lower) / 1.3)
    
    Do While diff
        high = lower + diff
        
        Do Until high > upper
            low = high - diff
            If Compar(low, high) Then
                Swap low, high
                
                If diff = 1 Then
                    If (low - diff) >= lower Then
                        high = low - 1
                    End If
                End If
            End If
            If Not Sorting Then Exit Do
            high = high + 1
        Loop
        If Not Sorting Then Exit Do
        diff = Int(CSng(diff) / 1.3)
    Loop
End Sub

Private Sub Count_Sort(ByVal lower%, ByVal upper%)
    Caption = "Count Sort"
    Dim j%, k%, incdec%
    ReDim cnt%(BARS * UNIT), tp!(BARS * UNIT)
    
    For j = lower To upper
        k = CInt(Shape1(j).Height)
        cnt(k) = cnt(k) + 1
        tp(k) = Shape1(j).Top
        
        ShowColor j, j, GREEN, GREEN
        If Not Sorting Then Exit For
        
        Shape1(j).Visible = False
        Pause
        If Not Sorting Then Exit For
    Next
    If Not Sorting Then Exit Sub
    j = 0

    If Ascend Then
        incdec = 1
    Else
        incdec = lower
        lower = upper
        upper = -incdec
        incdec = -1
    End If
        
    Do Until lower * incdec > upper
        Do Until cnt(j) <> 0
            j = j + 1
        Loop
        Do While cnt(j)
            With Shape1(lower)
                .Height = j
                .Top = tp(j)
                .Visible = True
            End With
            Pause
            If Not Sorting Then Exit Do
            
            ShowColor lower, lower, BLUE, BLUE
            If Not Sorting Then Exit Do
            
            lower = lower + incdec
            cnt(j) = cnt(j) - 1
        Loop
        If Not Sorting Then Exit Do
    Loop
End Sub

Private Sub Selection_Sort(ByVal lower%, ByVal upper%, _
                    Optional interpolating As Boolean)
    Caption = "Selection Sort"
    Dim j%, low%, high%
    
    Do Until lower >= upper
        If Compar(lower, upper) Then
            Swap lower, upper
        End If
        If Not Sorting Then Exit Do
        low = lower
        high = upper
        
        ShowColor low, high, ORANGE, ORANGE
        If Not Sorting Then Exit Do
        
        For j = lower + 1 To upper - 1
            If Compar(low, j) Then
                ShowColor j, low, ORANGE, BLUE
                low = j
            ElseIf Compar(j, high) Then
                ShowColor j, high, ORANGE, BLUE
                high = j
            End If
            If Not Sorting Then Exit For
        Next
        If Not Sorting Then Exit Do
        
        If high <> upper Then
            Swap high, upper
        Else
            ShowColor high, high, BLUE, BLUE
        End If
        If Not Sorting Then Exit Do
        
        If low <> lower Then
            Swap lower, low
        Else
            ShowColor low, low, BLUE, BLUE
        End If
        If Not Sorting Then Exit Do
        
        If interpolating Then Exit Do
        lower = lower + 1
        upper = upper - 1
    Loop
End Sub

Private Sub Interpolate_Sort(ByVal lower%, ByVal upper%)
    'First set the min and max at the ends
    Selection_Sort lower, upper, True
    
    Caption = "Interpolation Sort"
    ReDim previous%(lower To upper)
    Dim j%, k%, base%, offset%, dif1!, dif2!, try%

    dif1 = CSng(upper - lower)
    dif2 = CSng(Shape1(upper).Height - _
                Shape1(lower).Height)
    
    base = IIf(Ascend, lower, upper)
    j = lower + 1

    Do Until j >= upper
        If previous(j) = 0 Then
            offset = base + CInt(dif1 * _
                CSng(Shape1(j).Height - _
                    Shape1(base).Height) / dif2)
                    
            try = 1
            Do
                If offset <= lower Then
                    offset = lower + 1
                ElseIf offset >= upper Then
                    offset = upper - 1
                End If
            
                If previous(offset) = 0 Then
                    If j <> offset Then
                        ShowColor j, offset, GREEN, GREEN
                        If Not Sorting Then Exit Do
                        Swap j, offset
                        If Not Sorting Then Exit Do
                    End If
            
                    previous(offset) = -1
                    Exit Do
                ElseIf try = 1 Then
                    offset = offset + 1
                    try = 2
                ElseIf try = 2 Then
                    offset = offset - 2
                    try = 3
                Else
                    j = j + 1
                    Exit Do
                End If
            Loop
            If Not Sorting Then Exit Do
        Else
            j = j + 1
        End If
    Loop
    
    'Cleanup imprecision and duplicates
    'If Sorting Then Insertion2_Sort lower, upper
End Sub

Private Sub Merge_Sort(ByVal lower%, ByVal upper%)
    Caption = "Merge Sort"
    
    Select Case upper - lower
        Case Is <= 0: Exit Sub
        Case 1
            If Compar(lower, upper) Then
                Swap lower, upper
            End If
            Exit Sub
    End Select

    Dim j%, upper1%, lower1%
    upper1 = lower + (upper - lower) \ 2
    lower1 = upper1 + 1

    Merge_Sort lower, upper1
    If Not Sorting Then Exit Sub
    Merge_Sort lower1, upper
    If Not Sorting Then Exit Sub

    Do While lower1 <= upper And lower <= upper1
        If Compar(lower, lower1) Then
            Swap lower, lower1
            If Not Sorting Then Exit Do
                
            For j = lower1 To upper - 1
                If Compar(j, j + 1) Then
                    Swap j, j + 1
                    If Not Sorting Then Exit For
                Else
                    Exit For
                End If
            Next
            If Not Sorting Then Exit Do
        End If

        lower = lower + 1
    Loop
End Sub

Private Sub Quick_Sort(ByVal lower%, ByVal upper%)
    If lower >= upper Then Exit Sub
    
    Caption = "Quick Sort"
    Dim low%, midl%, high%, midval%
    
    low = lower
    high = upper
    
    midl = lower + (upper - lower) \ 2
    midval = Shape1(midl).Height
    Do While low <= high
        ShowColor midl, midl, ORANGE, ORANGE
        If Not Sorting Then Exit Do
        
        ShowColor low, high, GREEN, GREEN
        If Not Sorting Then Exit Do
 
        Do Until low >= upper
            If (Ascend And _
                Shape1(low).Height >= midval) Or _
                (Not Ascend And _
                Shape1(low).Height <= midval) Then _
                    Exit Do
            low = low + 1
            If low <= upper Then
                ShowColor low, low - 1, GREEN, BLUE
                If Not Sorting Then Exit Do
            End If
        Loop
        If Not Sorting Then Exit Do
     
        Do Until high <= lower
            If (Ascend And _
                midval >= Shape1(high).Height) Or _
                (Not Ascend And _
                midval <= Shape1(high).Height) Then _
                    Exit Do
            high = high - 1
            If high >= lower Then
                ShowColor high, high + 1, GREEN, BLUE
                If Not Sorting Then Exit Do
            End If
        Loop
        If Not Sorting Then Exit Do
 
        If low <= high Then
            If low < high Then
                Swap low, high
            Else
                ShowColor low, high, BLUE, BLUE
            End If
            If Not Sorting Then Exit Do
            low = low + 1
            high = high - 1
        Else
            Shape1(low).FillColor = BLUE
            Shape1(high).FillColor = BLUE
        End If
        
        Shape1(midl).FillColor = BLUE
    Loop

    If Sorting And lower < high Then Quick_Sort lower, high
    If Sorting And low < upper Then Quick_Sort low, upper
End Sub

Private Sub Heap_Sort(ByVal lower%, ByVal upper%)
    Caption = "Heap Sort"
    Dim j%, tmp%

    j = upper - (upper - lower) \ 2
    
    Do Until j >= upper
        SiftUp upper, j, lower
        If Not Sorting Then Exit Do
        j = j + 1
    Loop
    If Not Sorting Then Exit Sub

    For j = lower To upper - 1
        If Compar(j, upper) Then
            Swap j, upper
            If Not Sorting Then Exit For
            SiftUp upper, upper - 1, j
            If Not Sorting Then Exit For
        End If
    Next
End Sub
Private Sub SiftUp(first%, ByVal midl%, last%)
    Dim k&, k1%, m1%, tmp%
    
    k = CLng(midl - first) * 2 + first
    Do While k >= last
        If k > last Then
            k1 = k + 1
            If Compar(k1, k) Then
                ShowColor k1, k, BLUE, BLUE
                 k = k - 1
            End If
        End If
        
        k1 = k + 1
        m1 = midl + 1
        If Compar(m1, k1) Then
            Swap m1, k1
            If Not Sorting Then Exit Do
        Else
            Exit Do
        End If
        midl = k
        k = CLng(midl - first) * 2 + first
    Loop
End Sub

'=======================================================
'              Sort SubRoutines
'=======================================================
Private Function Compar(ByVal n1%, ByVal n2%) As Boolean
    If Not Sorting Then Exit Function
    
    Dim color1&, color2&
    color1 = Shape1(n1).FillColor
    color2 = Shape1(n2).FillColor
    ShowColor n1, n2, GREEN, GREEN
        
    If Not Sorting Then Exit Function
        
    If (Ascend And _
        Shape1(n1).Height > Shape1(n2).Height) Or _
        (Not Ascend And _
        Shape1(n1).Height < Shape1(n2).Height) Then
            Compar = True
    Else
        ShowColor n1, n2, color1, color2
    End If
End Function

Private Sub Swap(n1%, n2%)
    If Not Sorting Then Exit Sub
    Dim t1%, t2%
    
    ShowColor n1, n2, RED, RED
    If Not Sorting Then Exit Sub
    
    With Shape1(n1)
        t1 = .Height
        t2 = .Top
        .Height = Shape1(n2).Height
        .Top = Shape1(n2).Top
    End With
    
    With Shape1(n2)
        .Height = t1
        .Top = t2
    End With
    Pause
    
    If Sorting Then ShowColor n1, n2, BLUE, BLUE
End Sub

Private Sub ShowColor(ByVal num1%, ByVal num2%, _
        ByVal color1&, ByVal color2&)
    LabelCaption color1
    Shape1(num1).FillColor = color1
    Shape1(num2).FillColor = color2
    Pause
 End Sub

Private Sub Pause()
    Dim i&
    
    If Stepping Then
        Stepit = False
        Do Until Stepit
            DoEvents
        Loop
    Else
        For i = 1 To Spd
            If Not Sorting Then Exit For
            DoEvents
        Next
    End If
End Sub

Private Sub LabelCaption(ByVal clr&)
    Static s$
    
    Select Case clr
        Case RED: s = "Swap!"
        Case GREEN: s = "Comparing"
        Case ORANGE: s = "Min Mid Max"
        Case Else: clr = BLUE: s = "No Op"
    End Select
    
    Shape1(BARS).FillColor = clr
    Label1.Caption = s
End Sub

'=======================================================
'              Button Procedures
'=======================================================
Private Sub Command1_Click(Index%)
    Static idx%, i%
    
    If Sorting Then
        Select Case Index
            Case 0, 1
                Stepping = False
                Stepit = True
            Case 3
                If Stepping Then
                    Stepit = True
                Else
                    Stepp Index
                End If
                Exit Sub
        End Select
        
        Sorting = False
        Stepit = True
        idx = Index + 1
        Exit Sub
    End If

    BackColor = vbButtonShadow
    Label1.BackColor = vbButtonShadow

cm1: Sorting = True
    Select Case Index
        Case 0: Reset
        Case 1: Stopp
        Case 2: UpDn Index
        Case 3: Stepp Index: GoTo cm2
        Case 4: Bubble_Sort 0, BARSM1
        Case 5: BiBubble_Sort 0, BARSM1
        Case 6: Insertion_Sort 0, BARSM1
        Case 7: Shell_Sort 0, BARSM1
        Case 8: Count_Sort 0, BARSM1
        Case 9: Selection_Sort 0, BARSM1
        Case 10: Interpolate_Sort 0, BARSM1
        Case 11: Merge_Sort 0, BARSM1
        Case 12: Quick_Sort 0, BARSM1
        Case 13: Heap_Sort 0, BARSM1
    End Select
    
    If idx Then
        Index = idx - 1: idx = 0
        For i = 0 To BARSM1
            Shape1(i).FillColor = BLUE
        Next
        LabelCaption BLUE
        GoTo cm1
    ElseIf Index Then
        If Sorting Then
            BackColor = ORANGE
            Label1.BackColor = ORANGE
        End If
    End If
cm2: Sorting = False
End Sub

Private Sub HScroll1_Change()
    If Not Stepping Then Spd = IHI - HScroll1.Value
End Sub

Private Sub Stopp()
    Sorting = False
    Stepping = False
    Stepit = True
    HScroll1.Enabled = True
    Command1(2).Enabled = True
    Command1(3).Caption = "1"
    Command1(3).ToolTipText = "Click for Stepping"
    Caption = "Sort Viewer"
End Sub

Private Sub Stepp(i%)
    Command1(i).Caption = "+"
    Command1(i).ToolTipText = "Click to Step"
    Command1(2).Enabled = False
    HScroll1.Enabled = False
    Stepping = True
End Sub

Private Sub Reset()
    Dim nbrs As New Collection
    Dim i%, j%, k%
    
    Stopp
    
    For i = 0 To BARSM1
        nbrs.Add i
    Next
    
    Do While nbrs.Count
        Randomize
        i = Int(Rnd * nbrs.Count + 1)
        k = nbrs(i)
        nbrs.Remove i
        
        With Shape1(j)
            .Top = SLOC - UNIT * k
            .Height = UNIT * (k + 1)
            .FillColor = BLUE
            .Visible = True
        End With
        j = j + 1
    Loop
    LabelCaption BLUE
End Sub

Private Sub UpDn(i%)
    Sorting = False
    With Command1(i)
      If Ascend Then
        .Caption = "<"
        .ToolTipText = "Click for Ascending"
        Ascend = False
      Else
        .Caption = ">"
        .ToolTipText = "Click for Descending"
        Ascend = True
      End If
    End With
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    If Stepping Then
        KeyCode = vbKeyEscape
        Stepit = True
    End If
End Sub

'=======================================================
'              Initialization
'=======================================================
Private Sub Form_Load()
    Const SPCING! = 300
    Dim i%, tp!
    
    Sorts(0) = "Bubble"
    Sorts(1) = "Bi-Bub"
    Sorts(2) = "Insert"
    Sorts(3) = "Shell"
    Sorts(4) = "Count"
    Sorts(5) = "Select"
    Sorts(6) = "Interp"
    Sorts(7) = "Merge"
    Sorts(8) = "Quick"
    Sorts(9) = "Heap"
    
    Spd = MINSPEED
    Ascend = True
    KeyPreview = True
    BackColor = vbButtonShadow
    Width = SPCING * BARS + MARGIN * 2 + BOARDERS
    Height = UNIT * (BARS + 5) + MARGIN * 5 + _
                            BOARDERS + TITLEBAR
    
    tp = UNIT * (BARS + 4) + MARGIN * 4
    With HScroll1
        .Move MARGIN * 2 + UNIT * 1.5, tp, _
                UNIT * 6 + MARGIN, UNIT
        .Min = MINSPEED
        .Value = (IHI - MINSPEED) \ 2 + MINSPEED
        .SmallChange = 250
        .LargeChange = 1000
    End With
    
    With Label1
        .Move MARGIN * 4 + UNIT * 8, tp, _
                UNIT * 4.5, UNIT
        .BackColor = vbButtonShadow
        .Alignment = vbRightJustify
        .FontBold = True
    End With

    With Shape1(0)
        .FillStyle = vbFSSolid
        .Move MARGIN, SLOC, UNIT, UNIT
    End With
    
    For i = 1 To BARSM1
        Load Shape1(i)
        With Shape1(i)
            .Left = MARGIN + SPCING * i
            .Visible = True
        End With
    Next
    
    Load Shape1(i)
    With Shape1(i)
        .Move MARGIN * 5 + UNIT * 13, tp, UNIT * 4, UNIT
        .Visible = True
    End With
    
    AddButton "R", "Stop & Reset"
    AddButton "S", "Stop"
    AddButton ">", "Click for Descending"
    AddButton "1", "Click for Stepping"
    
    For i = 0 To MAXFUNC
        AddButton Sorts(i)
    Next
    
    Reset
End Sub

Private Sub AddButton(s$, Optional tip$ = "")
    Static num%, lt!, tp!, wd!, ht!

    If num Then Load Command1(num)
        
    Select Case num
        Case 0
            lt = MARGIN
            tp = SLOC + UNIT + MARGIN
            wd = UNIT * 1.5
            ht = UNIT
        Case Is < 4
            tp = tp + UNIT + MARGIN / 2
        Case 4
            lt = MARGIN * 2 + UNIT * 1.5
            tp = SLOC + UNIT + MARGIN
            wd = UNIT * 3
            ht = UNIT * 2
        Case 9
            lt = MARGIN * 2 + UNIT * 1.5
            tp = SLOC + UNIT * 3 + MARGIN * 2
        Case Else
            lt = lt + UNIT * 3 + MARGIN
    End Select
    
    With Command1(num)
        .Move lt, tp, wd, ht
        .Caption = s
        .ToolTipText = tip
        .FontBold = True
        .Visible = True
    End With
    
    num = num + 1
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Sorting = False
    Stepping = False
    Stepit = True
End Sub
Running the Viewer
Press F5 to run the viewer. The Form pictured above will appear and you will be able to watch the sorting by clicking on one of the ten methods.

At any time while the viewer is sorting you may change the method of sorting simply by clicking that method. The horizontal slide bar will control the speed. Additionally there are four small buttons on the left that control sorting operations as follows:


        [R]eset (randomize) the bars

        [S]top the sort (continue with any method)

        [>]ascending toggles with [<] descending

        [1]Single steps through the sort.
           Click this, then [+] will appear. Subsequent
           clicks to this [+] (or pressing a key) will
           single step through the respective sort.
           Click stop or reset to quit this mode.
These buttons allow some experimentation. You can, for instance, sort the bars then change from ascending to descending without randomizing. The subsequent sort will show you how the method behaves with an array that is totally unordered. Also resorting an array that is already sorted (in the same direction) might offer insight as to the viability of a sort method. (In an application a sort method may be called to reorder an array after only one element is added.)
Note: in the bottom right corner the key to the current operation
is displayed by the following color codes:


           BLUE            No Operation
           GREEN           Comparing
           RED             Swapping
           ORANGE          Minimum Midpoint or Maximum
Method Explanations
Bubble Sort
This sort makes a pass from low to high and simply compares one element with the element directly above it (swapping as necessary). You can see how it gets its name with the viewer as it eventually percolates or bubbles up the element with the maximum value to the highest position. Passes are continually made until a pass when it detects that no swaps have been made.

Since on each pass, the highest value is moved to the top you will see how each succeeding pass does not look at (compare) the previous high element. Although generally the bubble sort is the slowest, it is still frequently used because of the ease of programming it. Interestingly, it is one of the fastest sorts if very few elements are out of order. This may be useful in a situation where an array is to be ordered after the addition of a single element.

Bi-Bubble Sort
Imagine this sort like a bubble sort that goes in both directions. First the highest value is bubbled up and then the lowest element is bubbled down. This continues until no swaps are made in one direction. Note that in this sort both the highest and the lowest element of the previous pass need not be compared because it is guaranteed that the highest and the lowest will be bubbled into their respective end positions.
Insertion Sort
The insertion sort is one step up the evolution ladder from the bubble sort. It works by kind of a reverse osmosis process of making a single foreward pass but when encountering a lower value it will take it all the way back down to its lowest position. You will see that the one big adantage of this is that once that low element is dropped off in its lowest position, the sort resumes way back up where it first found that element.

It is faster than a bubble sort and may be the fastest of all if the array has only a few elements out of position.

Shell Sort
This might be considered an insertion sort that compares items further than one element apart. This distance or difference or offset is initially half the array in the classic shell sort, and is halved again on each subsequent pass. Also the routine classically continues carrying a swapped element down as far as possible like in an insertion sort.

In my study of this classic approach, the effect of continuing to carry down the swapped element proved to be very unproductive and time consuming, so I eliminated it until the difference gets to one (swapping contiguous elements). Also I found it considerably more effective to use a divisor of 1.3 instead of the classic 2.

This is still a popular sorting method because next to the heap sort this is the fastest and safest (with respect to the stack) sort.

Count Sort
This is an intriguingly simple and rocket fast sort. It makes two passes through the array. The first examines or counts the values, and the second pass distributes them.

To do this a second array is dimensioned large enough to contain all the possible values. The array position of this second array then becomes the value of the initial array and the element value of the second array becomes the number of elements of the initial array having that value.

While it is really beyond the scope of the viewer to include sort methods that involve multiple arrays (since you can't see the other array), this method seemed so novel to me and by far the fastest that I was compelled to include it.

Selection Sort
This is a double selection sort sometimes called a shaker sort. The routine simply goes through the whole array and finds the minimum and maximum values and then swaps them to the appropriate end positions (shaking them out to the ends). Subsequent passes do not have to look at these end positions previously placed. A classic selection sort finds only the minimum value in each pass.

Although interesting, it is not widey used since bubble and insertion sorts are easier to write and about as fast.

Interpolation Sort
This is totally my idea that I wrote from scratch after seeing the phrase interpolation sort but being unable to find a definition or an example on the net. First a single pass selection sort is performed to place minimum and maximum values. Then the positions of the remainder values are determined as a simple interpolation of these max and min values.

Only one pass can effectively be made to put as many elements as possible into the approximate position. Then a cleanup sort is required. It happens to work here without cleanup because the elements are evenly spaced. In the Sort Timer the cleanup is accomplished with an insertion sort.

Merge Sort
This sort is totally my idea that I wrote after viewing other so called merge sorts. Doubtless someone somewhere has invented it first. This is an inherently recursive sort which keeps halving the array and then works back up to merge the halves. Like the recursive quick sort, this may produce stack problems for very large arrays.
Quick Sort
The procedure chooses a mid element - with the hope that it will be a median value and then swaps elements on either side of it. Variations of this method attempt to choose median elements other than by midpoint. Still it is one of the fastest and most popular methods to be found with one significant drawback...

It is inherently recursive. That means that in order to accomplish its goal, it continues to call itself to sort parts of the array. At a point in the sort it may be hundreds of instances (calls) deep. When such a recursive call is made, parameter values and return addresses must be placed on the stack. Since the stack is a finite allocation of your computer's memory, this may prove to be a fatal problem with very large arrays.

Heap Sort
The technical discussion of this sorting method rapidly gets into vocabulary such as trees, nodes, leaves, parents, and children. Suffice it to say that the largest element is magically sifted down and then placed at the other end of the array, thus making the array to sort one element smaller on the next pass.

It seems like the oddest sort because you get the feeling that it is going in the wrong direction, and then suddenly it changes its mind and goes the right way. Fantastically it is one of the fastest sorts going and has no stack problem to worry about like the recursive quick sort. This combination has made it about the most popular with modern applications.

General Notes
Shuffling vs. Swapping
When you view many of these sorts you will note that it appears that most movement of elements is accomplished by a series of next element swaps.

A considerably faster method is to remember the first element as a temp variable, then shuffle or push up the contiguous elements by simple (single) assignment as long as the compare of the next element holds with the temp value. When the compare fails, the temp is placed in that ending position and the shuffle ends. Although it sounds complicated, it is rather simple once you get the idea and can save up to 66% of assignment operations (a swap takes three assignments). Several shuffle options may be seen in the Sorting Timer.

In Place Sorts
Note that all these sorts with the exception of the count sort are in place sorts. That is, they sort all the elements of the array within the bounds of the array. Except for the temp variable mentioned above, no additional elements or members are required. There are other sort methods that rely on dimensioning one or more additional arrays to hold the sorted or partially sorted results. One such sort would be a radix (or postman) sort. These are beyond the scope of this study since additional windows would have to be opened to show the additional arrays.