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 Computer Scientists call this a BUBBLE sort |
Like Playing Cards - for a medium job, like 50 pages Stack up the pages 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. Once you've finished that, split each pile into 10 piles, like
this: Once that is finished, take the 10 sheets in each pile
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)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
Selection Sort
Insertion Sort
Merge Sort