# Understanding the Need for Sorting Records

## Presentation on theme: "Understanding the Need for Sorting Records"— Presentation transcript:

Understanding the Need for Sorting Records
Sequential order: records are arranged based on the value in a field Examples: Student Id, Social Security Number, employee ID Random order: records are in the order in which they were added Sorting: placing the records in order, based on the values in one or more fields Ascending order: arranged from lowest to highest Descending order: arranged from highest to lowest

Understanding the Need for Sorting Records (continued)
Median value: the middle item when values are listed in order Mean value: arithmetic average Computer always sorts based on numeric values Character data is sorted by its numeric code value “A” is less than “B” Whether “A” is greater than “a” is system dependent

Understanding How to Swap Two Values
Swapping two values central to most sorting techniques When swapping two variables, reverse their position Use a temporary variable to hold one value

Figure 9-1 Program segment that swaps two values

Swap to number int main() { int No1,No2,temp; No1=6; No2=10; temp=No1; No1=No2; No2=temp; printf(“No1=%d”,No1); printf(“No2=%d”,No2); return 0; }

Using a Bubble Sort Bubble sort: one of the simplest sorting techniques Items in a list compared in pairs If an item is out of order, it swaps places with the item below it In an ascending sort, after a complete pass through the list: Largest item “sunk” to the bottom Smallest item “bubbled” to the top

Figure 9-2 The SortScores program

Program to sort test score
int main() { const int SIZE=5; int score[SIZE]; fillArray(score,SIZE); sortArray(score,SIZE); displayArray(score,SIZE); return 0; }

Figure 9-3 The fillArray() method

fillArray() method void fillArray(int score[],int size) { int i=0; while(i<size) printf(“Enter score”); scanf(“%d”,&score[i]; i=i+1; }

Figure 9-4 The incomplete sortArray() method

Incomplete sortArray() function
void sortArray(int score[],int SIZE) { int x=0; int comps=size-1; while(x<comps) if(score[x]>score[x+1]) swap(score,x); x=x+1; }

Figure 9-5 The swap() method

Swap() method void swap(int score[],int x) { int temp; temp=score[x+1]; score[x+1]=score[x]; score[x]=temp; }

Using a Bubble Sort (continued)
Start with x = 0, compare first pair and swap score[0] = 90 score[1] = 85 score[2] = 65 score[3] = 95 score[4] = 75 score[0] = 85 score[1] = 90 score[2] = 65 score[3] = 95 score[4] = 75 x = 1, compare with next and swap score[0] = 85 score[1] = 90 score[2] = 65 score[3] = 95 score[4] = 75 score[0] = 85 score[1] = 65 score[2] = 90 score[3] = 95 score[4] = 75

Using a Bubble Sort (continued)
With x = 2, no swap needed score[0] = 85 score[1] = 65 score[2] = 90 score[3] = 95 score[4] = 75 score[0] = 85 score[1] = 65 score[2] = 90 score[3] = 95 score[4] = 75 x = 3, compare with x = 4 and swap score[0] = 85 score[1] = 65 score[2] = 90 score[3] = 95 score[4] = 75 score[0] = 85 score[1] = 65 score[2] = 90 score[3] = 75 score[4] = 95

Figure 9-6 The completed sortArray() method

The complete sortArray()
void sortArray(int score[],int SIZE) { int x=0,y=0; int comps=size-1; while(y<comps) x=0; while(x<comps) if(score[x]>score[x+1]) swap(score,x); x=x+1; } y=y+1;

Using a Bubble Sort (continued)
Use nested loops for sorting an array Inner loop makes the pair comparisons Greatest number of comparisons is one less than the number of array elements Outer loop controls the number of times to process the list One less than the number of array elements

Figure 9-6 The displayArray() method

displayArray() void displayArray(int score[],int SIZE) { int x=0; while(x<SIZE); printf(“%d”,score[x]); x=x+1; }

Sorting a List of Variable Size
Use a variable to hold the number of array elements Declare the array with a large fixed size Count the number of input elements Store count in variable Use the variable to determine size of array

Refining the Bubble Sort by Reducing Unnecessary Comparisons
After the first pass through the array: Largest item must be at the end of array Second largest item must be at the second-to-last position in the array Not necessary to compare those two values again On each subsequent pass through the array, stop the pair comparisons one element sooner

Figure 9-9 Flowchart and pseudocode for sortArray() method using pairsToCompare variable

Refining the Bubble Sort by Eliminating Unnecessary Passes
Need one fewer pass than the number of elements to completely sort the array Can reduce the number of passes if array is somewhat ordered already Use a flag variable to indicate if there were any swaps during a single pass If no swaps, the array is completely sorted

Figure 9-10 Flowchart and pseudocode for sortArray() method using switchOccurred variable

Figure Flowchart and pseudocode for sortArray() method using switchOccurred variable (continued)

Using an Insertion Sort
Bubble sort is one of the least efficient sorting methods Insertion sort requires fewer comparisons Compare a pair of elements If an element is smaller than the previous one, search the array backward from that point Insert this element at the proper location

Figure 9-11 Movement of the value 75 to a “better” array position in an insertion point

Figure 9-12 Insertion sort

Using a Selection Sort Selection sort: two variables store the smallest value and its position in the array Store first element value and its position in variables Compare it to the next element If next element is smaller, put its value and position in the variables Continue until end of array, at which time the smallest value and its position are in the variables Swap the first element value and position with the element and position stored in the variables

Using a Selection Sort (continued)
Start at the second element in the array and repeat the process Continue until all elements except the last have been designated as the starting point After making one fewer pass than the number of elements, the array is sorted

Figure 9-13 A selection sort method

Figure 9-13 A selection sort method (continued)

Using Multidimensional Arrays
One-dimensional (single-dimensional) array Represents a single list of values Multidimensional array A list with two or more related values in each position Two-dimensional array Represents values in a table or grid containing rows and columns Requires two subscripts Three-dimensional array Supported by many programming languages Requires three subscripts

Figure 9-14 View of a single-dimensional array in memory

Table 9-1 Rent schedule based on floor and number of bedrooms

Figure 9-15 Two-dimensional rent array based on rent schedule in Table 9-1

Figure 9-16 A program that determines rents

Using a Built-In ARRAY Class
Similar tasks frequently performed on different arrays Example: filling and sorting Modern programming languages provide an Array class Newer languages have vast libraries Contain built-in methods Most useful Array class contains overloaded versions of each method call for each data type Different versions of sort() for numeric and string elements If no Array class available, write your own

Table 9-2 Typical useful methods of the Array class

Using Indexed Files Large data file to be accessed in sorted order; usually one field determines the sorting Key field: field whose contents make the record unique Indexing records: list of key fields paired with corresponding file position Sorting indexes faster than physically sorting actual records Random-access storage device: records accessed in any order

Table 9-3 Sample index

Using Indexed Files (continued)
Address: location within computer memory or storage Every data record on disk has an address Index: holds physical addresses and key field values Data file in physical order Index sorted in logical order When a record is removed from an indexed file, deleted from the index file Not physically removed

Using Linked Lists Linked list: requires one extra field in every record Holds the physical address of next logical record Add a new record: Search the linked list for the correct logical location Insert the new record Link previous record to new record Link new record to next record Delete a record by unlinking it More sophisticated linked lists store a next and a previous field Traversed both forward and backward

Table 9-4 Sample linked customer list

Table 9-5 Updated customer list

Summary Sort data records based on the contents of one or more fields
Ascending order Descending order Swap two values Temporary variable holds one of the values Bubble sort Compares pairs Swaps with item below Ascending bubble sort, largest item sinks to bottom Smallest item rises to the top

Summary (continued) Bubble sort (continued)
Eliminate unnecessary comparisons in each pass and eliminate unnecessary passes to improve bubble sort Size of list to be sorted may vary Count values to be sorted Initialize array with count variable when value is known Bubble sort improved by stopping comparisons one element sooner on each pass Bubble sort improved by stopping when all items sorted Flag variable indicates when any item is swapped Indicates when no items swapped in one pass through

Summary (continued) Insertion sort usually requires fewer comparisons
Pairwise comparisons Locate out-of-order element Search array backward to a smaller element Move each element down one Insert out-of-order element into open position

Summary (continued) Ascending selection sort
First element assumed to be smallest Value and position stored in variables Every subsequent element tested Smallest element found, value and position saved Lowest value in first position after one pass through array

Summary (continued) One-dimensional array
Also called single-dimensional array Single column of values Accessed with a single subscript Most object-oriented languages support two-dimensional arrays Both rows and columns Two subscripts

Summary (continued) Array class contains useful methods for manipulating arrays Indexed files access data records in a logical order Differs from physical order Must identify key field for each record Linked list contains extra field within every record Extra field holds physical address of next logical record