It is easy to imagine a situation in which a user has an array of numbers that need to be sorted from smallest to largest. If you've ever alphabetized a stack of homework papers or sorted exams by score you will know that there are many different ways to do this. A few methods include:
Some of these algorithms work well when made into computer programs, some don't. Since computers must frequently sort lists of data as large as a phone book, programmers try to use the fastest algorithm they can find which doesn't also require the computer to have large amounts of memory (laying all the homework papers out on the surface of your desk and then picking them up in order might be a fast way to sort them, but you probably don't have enough desk space to try this!)
In this exercise we learn about a moderately fast sort algorithm called an insertion sort. You can see how it compares to four other sort algorithms by viewing the web page at http://max.cs.kzoo.edu/~abrady/java/sorting/.
Imagine you have a stack of exams which you want to sort so that the exam with the lowest score is on the top of the stack and the one with the highest score is at the bottom. To use the insertion sort algorithm to do this you could:
Once you understand how the insertion sort algorithm works for papers you can start thinking about how to write a LOGO program to sort numbers. The stack of exams becomes an array whose elements are the exam score. Your "index finger" is like the index of a FOR command; as each exam is sorted your index finger comes to lie above the next exam to be sorted. The score of the exam being sorted can be stored as a separate variable for ease of comparision. Moving an exam from the top of the lower stack into the upper stack changes the position of all the scores in the array that were between where it started and where it was moved to. (Be sure you understand this last sentence. It's a simple idea that is very important in programming the algorithm!)
This description of the insertion sort is enough for most people, but not for a computer. Below is an even more detailed description of the insertion sort algorithm, written more terms of arrays and variables.
In practice it is difficulty to write good pseudocode, and it can also be difficult to translate pseudocode into LOGO. Statements like "insert the new one below it" require some interpretation. (Will the index of that location be I, I-1 or I+1?)
Although the insertion sort is a simple and natural method of sorting, it is surprisingly difficult to program. It helps to break the program into two parts; one part to handle the insertion of a value into the sorted portion of the list and one part to step through all the values needing sorting.
An example of a LOGO program that implements the insertion sort algorithm is given below. This is not the only way to implement this algorithm. Could you suggest any improvements to the code below?
TO INSERTIONSORT :ARRAY
PRINT [The unsorted array is:]
PRINT :ARRAY
FOR [INDEX 2 [COUNT :ARRAY]] [ ; Go through each item in the array. ~
MAKE "NEWSCORE (ITEM :INDEX :ARRAY)
MAKE "POINTER :INDEX - 1 ; Points to end of sorted subarray.
INSERT :POINTER :NEWSCORE :ARRAY ; Insert new item in sorted subarray.
PRINT (SENTENCE [After sorting the first] :INDEX [values, the array is:])
PRINT :ARRAY
]
PRINT [The sorted array is:]
PRINT :ARRAY
END
TO INSERT :I :SCORE :SCORES ; Inserts value in sorted subarray.
WHILE [((ITEM :I :SCORES) > :SCORE)][ ; Look until values are below :SCORE ~
; Move old values down by one.
SETITEM :I+1 :SCORES (ITEM :I :SCORES)
MAKE "I :I-1 ; Move to next value in the array.
IF :I < 1 [ ; If we've looked at all the values, ~
SETITEM 1 :SCORES :SCORE ; insert :SCORE at start of array
STOP ; and stop.
]
]
SETITEM :I+1 :SCORES :SCORE ; Insert :SCORE in its proper place.
END
; It may be possible to rewrite this in a simpler way.
Paste this program into LOGO and run the insertion sort a few times
with a few different input arrays. For example:
MAKE "ARRAY1 {1 3 7 5 4 2}
INSERTIONSORT :ARRAY1
Carefully read the output of the program. Compare the changing values
in the array to the changing order of the exams in a stack of papers
being sorted. The print statements provide a window into the workings
of the program. This helps us see how the program works, and helps
even more when trying to figure out why a program doesn't work!Once you think you understand how the program works, try running the INSERT procedure independently of the INSERTIONSORT procedure. What arguments do you need to provide to it? In a non-working program that uses different sub-procedures, we can test each procedure individually to find the problem. This is one way in which modular programming makes a programming simpler and more efficient.