2d+Arrays_Sorting

ASSIGNMENT #11

ONE DIMENSIONAL ARRAYS: Part 2: Sorting & Searching

Background

The Bubble Sort, or Selection Sort: records within a data file may not be in any particular order. This makes it harder for wetware (humans) to use the data efficiently. Humans like order (bedrooms and desks aside). One method of create order out of chaos is the Bubble Sort. This type of sort is accomplished by comparing the first two items in a list and interchanging their positions if they are not in ascending order of size. This process is repeated using the second and third items, then the thrd and fourth, etc., When finished, the largest item will be in the final position. The whole process is repeated ad nauseum until the entire list is in ascending order. In order to do a Bubble sort (or indeed a sort of any kind) the entire list must be read into an array. Once in the array the sort can begin. Here's a graphic display of the Bubble sort.

media type="file" key="Bubble_Sort.mp4"

As you can see, if you had several hundred or thousand records in a database, the Bubble would take a mighty long time. The essential difficulty with sorting is that only one value can be in a single memory location at any one time. It's like musical chairs, say you have three people (values) and only two chairs (memory locations). So how can you swap values? The answer is... you add another chair. In sorting, you need a temporary variable to hold one of the values while the swap is being made. Its somewhat analogous to weaving or braiding.

The values being sorted weave in and out of the temporary holding variable, allowing the sort to take place.

Sorting and search have a lot in common, particularly with the bubble sort, it each a comparision is made. In the case of the bubble sort you are comparing each record with its neighbour such that the smallest record will "bubble" up to the top of the stack.

In a linear search, each record in turn is compared to the search criteria. Which, as we have seen with the bubble sort, is fine for small files, but really slow for longer ones. For instance, in a linear search if there are N records, there will be an average of N/2 comparisons made by the computer. Sometimes, if the desired record is the first in the list, onely one comparison will be made.... but what if the record you're looking for is the last one? Imagine a datafile with 100000 records in it. Task

1) York Region Police want a list of those criminals who have been convicted of Theft, Homicide, and Arson. The file created should contain the Name, Offence and Conviction Date of each criminal. The offences are to be listed as letter codes T for theft, A for Arson, etc. The output to screen should list the offences written out in full, i.e., Y.R.P.D. Criminal Listings Name Offence Conviction Date Jim Jones Theft April 19, 1990 Jane Adams Arson January 15, 1993 The program must be menu driven with a minimum of 30 records. SELECT AN OPTION 1) MASTER LISTING 2) ARSON 3) THEFT 4) HOMICIDE 5) ENTER NEW CONVICT 6) SORT 7) SEARCH FOR A RECORD 8) EXIT

1b) Modify 1a) such that when a new convict is entered, the datafile does not have to be overwritten.&#0;&#0;

2) Modify Assignment 10.3 such that the user may choose between the following options

1) SEE THE ERROR REPORT 2) DAYS WITH ACID RAIN BELOW 4 3) DAYS OF THE MONTH WITH GREATER THAT DEGREES DIFFERENCE BETWEEN THE LOW AND HIGH TEMPERATURES 4) DAYS OF THE MONTH WITH RAINFALL MORE THAN 5 FROM HIGHEST TO LOWEST 5) EXIT THE PROGRAM

3) Modify 2) such that it has a GUI.

4) Bonus Challenge: Research and program a faster sorting algorithm. There are 9 that I know of and Bubble sort is the slowest. Develop a datafile of 500 records. Compare the two sort routines to see which is faster. Now display each sort graphically as it is running so that a user can literally see the difference in the way each algorithm functions.