VCE IT Lecture Notes by Mark Kelly, McKinnon Secondary College
Searching and Sorting |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SD U4O1 KK13- techniques for searching, including binary search, and techniques for sorting, including bubble sort and quick sort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It's ironic. The most computationally demanding tasks a computer can do are searching and sorting. They are also the most common operations done by computers. It's no surprise then that programmers have for decades sought to discover more and more efficient ways to search and sort data. Some programmers have spent the bulk of their career researching search and sort algorithms. Don't expect this page to neatly tell you everything you need to know about them: they are complete sciences in themselves. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Searching |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let's take the case of an array or sequential file of N text (student IDs and corresponding test results) items in random order.
We want to find the location of a particular item in the list. Using brute force (any technique that relies on effort instead of elegance) we could look at each item in the list and compare it with the item we are searching for. Let's search for BLA0008. Reading from the top of the list, we would need to check 16 IDs before we finally found the one we were looking for. If we'd been looking for HOS0004, we would've lucked out and only needed 2 comparisons. So, for a random list of N items, the number of comparisons required is anywhere between 1 and N. This could be very bad if the list has millions of entries. And if the item being searched for is not guaranteed to be in the list at all (e.g. you're searching a list of registered users to see if an ID is in the list), it will always take N comparisons before it reported that the item is not in the list. Since there is no order in the list, there is no possible strategy to search smarter. You can't overcome the massive power of randomness. So you need to introduce some order into the list in order to use some brains during the search. This means sorting the list. How we can sort the list will remain a mystery for a while.
Now we can use some tricky techniques. Such tricky techniques are called algorithms - they describe the strategy used to solve a problem. At least now we can find whether a designated item is or is not in the list without always having to check every item first. How? Think about it before proceeding. Here's some thinking animation...
Let's assume we're searching to find out if KEL0001 is in the list. Starting at the top, we compare items down to HOS0004. The next item in the sorted list is KIN0005, which is alphabetically after KEL0001. Since KEL0001 was not found by the time we reached KIN0005, we know it's not in the list and we can stop searching after only 8 items instead of having to process all 16. That's a saving, but we can do better. Let's try a binary search. Have you ever played the game, "I'm thinking of a number between 1 and 100"? Each time you guess, the person tells you if your guess is high or low or correct. With brute force (sheer guessing), you'd get the right answer in 1 to 100 moves. There's a smarter way. With each guess you can halve the number of possible choices. At each guess we revise the upper and lower limits of possible answers and the next guess is precisely half way between the limits. When we start, the lower limit is 1 and the upper limit is 100, so we guess halfway between the those...
So, instead of up to 100 random guesses, we can always get the answer within 5 to 7 guesses. That's an improvement. In pseudocode it might look like this: DECLARE upperlimit, lowerlimit upperlimit = 100 WHILE success = FALSE IF guess = valuetofind THEN PRINT "Got it in " & count & " tries!"
In our original test, let's search for DIP0001 in the sorted list.
Lower limit = 1. Upper limit = 7. Halfway guess = 4. Value is CLA0001. Too low.
Lower limit = 5. Upper limit = 7. Halfway guess = 6. Value is GIB0002. Too high.
Lower limit = 5. Upper limit = 7. Halfway guess = 6. Value is GIB0002. Too high. Lower limit =5. Upper limit =5. We have a winner. Now we can deal with record 5 in whatever way we need to. Good old binary search. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sorting |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Such searching relies on the data being sorted, and sorting is even more challenging than searching since it can require a lot of data copying and moving to get things in order. Moving data in RAM is slow enough; moving it on a disk is very slow. So you want to sort as cleverly as you can to minimise the total number of data movements. There are many different sorting algorithms - some are better in some circumstances (e.g. how random the original data is) than others. They include
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Bubble sort - thanks to Wikipedia for help |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Bubble sort is a simple algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. It is probably the least efficient sorting algorithm. Let us take the array of numbers "5 1 4 2 8", and sort them array from smallest to biggest using bubble sort algorithm. First Pass: Bubble sort is simple to code, but awfully inefficient to use, and it gets exponentially worse as the number of items to sort increases. It has been called a "perversely awful algorithm" and "it has nothing to recommend it". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Quicksort |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists. The steps are:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BOGOSORT - If bogosort were used to sort a deck of cards, it would consist of checking if the deck were in order, and if it were not, one would throw the deck into the air, pick the cards up at random, and repeat the process until the deck turns out to be sorted. The bogosort is not be recommended in a Software Development outcome.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| As an alternative to slow swapping of list items, also see my pages on binary trees and indexing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If you're into sorting theory (and have a PhD in maths), check out Wikipedia to learn about ...
|
Back to the IT Lecture Notes index
Back to the last page you visited
Created 16 Sep 2010
Last changed: September 17, 2010 1:07 PM
VCE IT Lecture notes copyright © Mark Kelly 2001-