Sorting

Sorting means putting things in order.  For example, if you drop a stack of paper on the floor, and the pages are all mixed up, you need to put them in order again.

There are a variety of algorithms (methods) for accomplishing this task.  We'll start with typical human algorithms, written in human terms (pseudo-code).  

Swapping Pairs - for a small job, like 10 pages

Stack up the pages
Look at the first two pages and swap if necessary
Look at the next two pages and swap if necessary
Continue to the end
Go back to the beginning and all the way through again
Repeat another pass through all the pages
Quit if you get all the way through without swapping

Computer Scientists call this a BUBBLE sort

Like Playing Cards  - for a medium job, like 50 pages

Stack up the pages
Look through them one at a time
Take each page and insert it in rougly the right place
At the end, check that they are actually sorted by
   reading the page numbers from 1 to 50

Computer Scientists call this an INSERTION sort

 

Categories - for a REALLY BIG job, like 500 pages

Think of several categories, like 0-99, 100-199, 200-299, etc.
Get 5 boxes (or make 5 piles.)
Go through the pages one at a time, and put them in the right box.

Once you've finished that, split each pile into 10 piles, like this:
   0-100 ==>  0-9, 10-19, 21-29....

Once that is finished, take the 10 sheets in each pile
   and "just" put them in order.

Computer Scientists call this a RADIX sort

You'll only really understand this problem if you do it once.  Maybe you can get your teacher to throw some papers on the floor.  Otherwise, try some of these "real world" problems:

Do you use the same (or similar) methods for each problem?  Or is there a best method for each problem?

Simple Sort Algorithm

In a computer (program), things aren't written on pieces of paper.  And you can't "look" at all the data at once.  Computers must look at just one or two things at a time and compare them.  Here is an example program implementing an algorithm called a Simple Sort.  That name is used because the program is very simple.  A human being probably would not sort anything this way.

Psuedocode

    Compare each item to every other item
    Swap them if they are out of order
    Keep going until you compared every position to every other position

Java Code for Simple Sort

  for(int c=0; c < nums.length; c = c+1)
  {
     for(int x=0 ; x < nums.length; x = x+1)
     {
         if (nums[c] > nums[x])      // if out of order
         {
         
  int temp = nums[c];      // swap the two items
            nums[c] = nums[x];
            nums[x] = temp;
         }    
     }
  }

Start with the array {6,8,4,5} and trace the code above (on paper).  You will need a lot of paper, and you need to write down the array each time it changes.  The following table will help:

c

x

nums[c] > nums[x]

nums[ ]

 

 

 

{ 6 , 8 , 4 , 5 }

0

 

 

 

 

0

6 > 6 ?   false

 

 

1

6 > 8 ?  false

 

 

2

6 > 4 ? true   

 { 4 , 8 , 6 , 5 }

 

3

4 > 5 ?   false

 

1

 

 

 

 

0

 

 

 

1

 

 

 

2

 

 

 

3

 

 

2

 

 

 

 

0

 

 

 

1

 

 

 

2

 

 

 

3

 

 

3

 

 

 

 

0

 

 

 

1

 

 

 

2

 

 

 

3

 

 

Now you can download the following program and run it, to see whether you were correct:   SimpleSort.java
Try some longer arrays, like with 10 numbers, and see whether it "always" works correctly.

This Simple Sort algorithm does seem to work, but there is probably no proof that it ALWAYS works.  It is easy to write, but it does a lot of extra comparisons.  That is both inefficient and perhaps unreliable.  There are several better algorithms, as listed below.

REAL Sorting Algorithms

We will look at 4 standard sorting algorithms

The first three are written with doubly nested loops, so they are about equally fast.

The Quick Sort is a lot more complex, but much faster.  IB SL students don't need to be able to program this, but we will have a look at how it works.

ACTION!

The following demonstration Applet shows the different algorithms in action:  http://math.hws.edu/TMCM/java/xSortLab/  

You need to watch each algorithm enough times until you can predict what will happen next at each step.  Spend some time on this.  Start with the Bubble Sort, then move on to the tasks below.  Then come back and watch the Selection Sort in action and do the tasks below.  Then do the Insertion Sort.

READING

Look at the IBO standard algorithms here :  http://ibcomp.fis.edu/ibo/Algorithms.htm  

Bubble Sort

  1. Watch the demo Applet until you KNOW how the Bubble Sort works.  
  2. Starting with the array  {3 , 4 , 1 , 5 , 2}, assume the Bubble Sort swaps larger values toward the right.
    Write down EXACTLY what changes will occur, in the correct order.
  3. Take the Simple Sort as a starting point.  Change the if... command so that it compares  nums[x] > nums[x + 1] .
    Then it is always comparing neighbors.  Get it to work correctly to produce a sorted list at the end.
    You need to write the loops a bit differently to make it work.
  4. Once you get it working, try a few different sized lists - 10 items, 20 items, 30 itmes.

Selection Sort

  1. Watch the demo Applet until you KNOW how the Selection Sort works.  
  2. Starting with the array  {3 , 4 , 1 , 5 , 2}, assume the Selection Sort swaps larger values toward the right.
    Write down EXACTLY what changes will occur, in the correct order.
  3. Take the Simple Sort as a starting point.  The x loop needs to search for the LARGEST (or smallest)
    item in the list.  Make it do that.  Then it needs to SWAP with the first (or second or third).
    Get it to work correctly to produce a sorted list at the end.
  4. Once you get it working, try a few different sized lists - 10 items, 20 items, 30 itmes.

Insertion Sort

  1. Watch the demo Applet until you KNOW how the Insertion Sort works.  
  2. Starting with the array  {3 , 4 , 1 , 5 , 2}, assume the Insertion Sort swaps larger values toward the right.
    Write down EXACTLY what changes will occur, in the correct order.
  3. Find a "correct" Insertion Sort algorithm as Java code - search the web.  Put it into your program
    and get it working correctly.

Merge Sort