CSC 150 : Sorting Algorithms
primary information resource: An Invitation to Computer Science, G. Michael Schneider and Judith L. Gersting, Brooks/Cole, 2000.

[ back to chapter 3 | back to search | schedule | on to chapter 4 ]

Topic Index
[ sorting | selection sort | bubble sort | insertion sort | quick sort ]


Sorting algorithms

No class of algorithms has been more closely studied and refined with respect to order of magnitude runtimes than algorithms for sorting data (arranging data in ascending or descending order).

The textbook explains one sorting algorithm, the selection sort.  In Lab 4, you will explore this plus two additional algorithms, the bubble sort and the quick sort.  In Lab 6, you will explore all three of these plus a fourth, the insertion sort.

See http://www.brian-borowski.com/Software/Sorting/ for a very nice animated demonstrations of a variety of sorting algorithms (all four of the above plus mergesort, heapsort, and shellsort).


Selection Sort

Study selection sort algorithm to understand it, determine which steps are worth counting, and do some runtime analysis.

Essence of selection sort for list of n values:


At the end of any pas through the list, list is partitioned into two parts: the unsorted part followed by the sorted part.

The sorted part starts at the end and grows toward the beginning.  You are finished when it reaches the beginning.

Note this uses the “search for the largest” algorithm from lab 3.

What operations appear to be most significant?
Comparisons (to find the largest) and exchanges.

Runtime analysis of selection sort.



Bubble sort

Essence of bubble sort for list of n values V1 through Vn:


See where it gets its name?  In the first pass, the largest value “bubbles” its way to its correct position.

At the end of any given pass, the list is partitioned into the unsorted part followed by the sorted part.

Runtime analysis of bubble sort.

NOTE: Best case can be reduced to n-1 comparisons and 0 exchanges using this simple optimization: set “exchanged” flag to false at beginning of pass and set it true if exchange is made.  If still false at end of pass then list is ordered so stop.  In the best case, list is originally ordered, so it stops after first pass.


Insertion Sort

Informal description:


Example of how a given pass works:
Consider algorithm in progress just before starting pass 4 with list:
7  12  21  24  |  15  5  32 (vertical bar separates sorted / unsorted part)
Select the value 15 and work backwards comparing and exchanging as you go until correct position is found (between the 12 and 21).
The 24 is compared to 15.  15 is smaller, so exchange them.
7  12  21  15  24 |  5  32
The 21 is compared to 15.  15 is smaller, so exchange them.
7  12  15  21  24 |  5  32
The 12 is compared to 15.  15 is larger, no exchange pass 4 is complete.

Informal worst case analysis:



Breaking the O(n2) barrier: Quick Sort

Note: many sorting algorithms are fundamentally faster than O(n2) but we choose Quick Sort because you will utilize it in labs 5 and 6.

Breaking the barrier usually requires a “divide and conquer” strategy.

Informal description of Quick Sort operating under ideal conditions:

The runtime analysis is complicated, because the Quick Sort algorithm actually calls itself twice!  We call this recursion.

Ideally, each sublist will be about half the size of the original.  This is the “divide and conquer” part.  With each recursive call, the sublists get smaller and smaller until they are a lot of little two-value sublists, each of which can be quickly sorted!  Then it all gets put back together again.

Complicated?  You bet.  The results are well worth it.  For each level of recursion, O(n) work is required (the rearranging-around-the-pivot part).  The number of levels of recursion is approximately the logarithm base 2 of n.  The total work is thus O( log2 n * n ).
So comparing Selection sort with Quick sort for n=1000, selection sort requires O(n2) or about 1,000,000 operations where Quick sort requires only about 10,000!  (log2 of 1000 is about 10)  Or 100 times faster!
For n=1,000,000 the savings are even better, Quick sort is 500,000 times faster than selection sort!  (log2 of 1,000,000 is about 20).  No wonder they call it “quick”!


[ C SC 150 | Peter Sanderson | Math Sciences server  | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson (PSanderson@otterbein.edu)