Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda
Topics 9.3 Introduction to Sorting Algorithms 9.4 Sorting an Array of Objects 9.5 Sorting Vectors
9.3 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here Bubble sort Selection sort Insertion sort
Selection Sort Algorithm Locate the smallest element in the array and exchange it with the element in position 0. Locate the next smallest element in the array and exchange it with element in position 1. Continue until all of the elements are in order. See pr9-05.cpp
Selection Sort Example Array numlist contains The smallest element is 2. Exchange 2 with the element at subscript 0. 11 2 29 3 Now in order 2 11 29 3
Selection Sort Example, Continued 2 11 29 3 The next smallest element is 3. Exchange 3 with the element at subscript 1. The next smallest element is 11. Exchange 11 with the element at subscript 2. 2 3 29 11 2 3 11 29
//Select sort puts elements in a in ascending order, by Chapter 9 slide 1212 //Select sort puts elements in a in ascending order, by //repeatedly finding minimum element and swapping it with //first element in “unsorted part” of array void selectSort (ArrayType a; int n) { int mindex; for (int i = 0; i < n-1; i++) mindex = i; //find index of min element in unsorted subarray for (int j = i+1; j < n-1; j++) if (a[j] < a[mindex]) mindex = j; } //swap 1st unsorted element with minimum element swap (a[mindex], a[i]);
Analysis of Sort Efficiency or Time Complexity Chapter 9 slide 1313 Analysis of Sort Notice: N elements N-1 passes In EACH iteration, found smallest element in unsorted part of list and put it in its correct place on each pass, 1 element put in its proper place Efficiency or Time Complexity 1ST Pass: N-1 comparisons 2nd Pass: N-2 comparisons … (N-1)st Pass: 1 comparison (N-1) + (N-2) + …+ 1 = N (N-1) = N2-N = O (N2) 2 2
Selection Sort Tradeoffs Chapter 9 slide 1414 Selection Sort Tradeoffs Benefit Easy to understand Uses FindMax or FindMin algorithm from CMPS1044 Disadvantage There are better ways to sort, esp. if you know something about your data
9.4 Sorting an Array of Objects As with searching, arrays to be sorted can contain objects or structures The key field determines how the structures or objects will be ordered When exchanging the contents of array elements, entire structures or objects must be exchanged, not just the key fields in the structures or objects See pr9-06.cpp
Insertion Sort Good when data is random, great when data is already somewhat ordered…. Algorithm: Consider first element is in sorted part of list for the next element in unsorted part of list put it into its proper place relative to the sorted part of the list Chapter 9 slide 16
Example 20 20 15 15 15 20 18 18 18 8 8 3 9 9 9 initial start end 1st pass 15 15 15 20 18 18 18 20 20 8 8 3 9 9 9 start end 2nd pass sorted unsorted Chapter 9 slide 17
Example 8 8 8 8 8 15 15 15 9 9 18 18 9 15 15 20 9 18 18 18 9 20 20 20 20 start end 4th pass sorted unsorted 15 15 15 8 18 18 8 15 20 8 18 18 8 20 20 20 9 9 9 9 start end 3rd pass Chapter 9 slide 18
void insertSort (ArrayType a; int n) { bool found; int j; //Insert sort puts elements in ascending order by repeatedly //putting 1st element in unsorted subarray into proper position of sorted array void insertSort (ArrayType a; int n) { bool found; int j; //put each element in unsorted subarray in proper position for (int i=1; i < n; i++) //find proper place for a[i] relative to a[0]..a[i] found = false; j = i; while ((j > i) && !found) //swap and decrement j if a[j] < a[j-1] swap (a[j], a[j-1]); j= j-1; } else found = true; } //end while } //end for } //end insertSort
Best Case Analysis- Data Ordered Pass # comparisons 1 1 2 1 3 1 … N-1 1 1 + 1 + .. + 1+ 1 = (N-1) * 1 = N-1 = O(N) Chapter 9 slide 20
Worst Case Analysis Pass # comparisons 1 1 2 2 3 3 … N-1 N-1 1 + 2 + .. + (N-2) + (N-1) = O (N2) Chapter 9 slide 21
Efficiency Sorting is a COMMON operation to ignore efficiency of sort is to ignore efficiency of program Level of efficiency measured by # of comparisons (dominant operation) # of comparisons is a function of n Chapter 9 slide 22
9.5 Sorting Vectors Sorting algorithms can be applied to vectors as well as to arrays You need slight modifications to functions to use vector arguments vector <type> & used in prototype There is no need to indicate the vector size, as functions can use the vector’s size member function See pr9-07.cpp
Other Resources on Sorting Sorting Algorithms Animations HTML5 Canvas Demo: Sorting Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda