Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
Figure 9-1 Program segment that swaps two values
5
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; }
6
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
7
Figure 9-2 The SortScores program
8
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; }
9
Figure 9-3 The fillArray() method
10
fillArray() method void fillArray(int score[],int size) { int i=0; while(i<size) printf(“Enter score”); scanf(“%d”,&score[i]; i=i+1; }
11
Figure 9-4 The incomplete sortArray() method
12
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; }
13
Figure 9-5 The swap() method
14
Swap() method void swap(int score[],int x) { int temp; temp=score[x+1]; score[x+1]=score[x]; score[x]=temp; }
15
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
16
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
17
Figure 9-6 The completed sortArray() method
18
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;
19
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
20
Figure 9-6 The displayArray() method
21
displayArray() void displayArray(int score[],int SIZE) { int x=0; while(x<SIZE); printf(“%d”,score[x]); x=x+1; }
22
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
23
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
24
Figure 9-9 Flowchart and pseudocode for sortArray() method using pairsToCompare variable
25
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
26
Figure 9-10 Flowchart and pseudocode for sortArray() method using switchOccurred variable
27
Figure Flowchart and pseudocode for sortArray() method using switchOccurred variable (continued)
28
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
29
Figure 9-11 Movement of the value 75 to a “better” array position in an insertion point
30
Figure 9-12 Insertion sort
31
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
32
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
33
Figure 9-13 A selection sort method
34
Figure 9-13 A selection sort method (continued)
35
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
36
Figure 9-14 View of a single-dimensional array in memory
37
Table 9-1 Rent schedule based on floor and number of bedrooms
38
Figure 9-15 Two-dimensional rent array based on rent schedule in Table 9-1
39
Figure 9-16 A program that determines rents
40
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
41
Table 9-2 Typical useful methods of the Array class
42
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
43
Table 9-3 Sample index
44
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
45
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
46
Table 9-4 Sample linked customer list
47
Table 9-5 Updated customer list
48
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
49
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
50
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
51
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
52
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
53
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.