Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alternate Version of STARTING OUT WITH C++ 4th Edition

Similar presentations


Presentation on theme: "Alternate Version of STARTING OUT WITH C++ 4th Edition"— Presentation transcript:

1 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 9 Searching and Sorting Arrays Copyright 2004 Scott/Jones Publishing

2 Topics 9.3 Introduction to Sorting Algorithms Chapter 9 slide 2

3 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 Chapter 9 slide 3

4 Chapter 9 slide 4

5 Bubble Sort Algorithm Compare 1st two elements and exchange them if they are out of order. Move down one element and compare 2nd and 3rd elements. Exchange if necessary. Continue until end of array. Pass through array again, repeating process and exchanging as necessary. Repeat until a pass is made with no exchanges. Chapter 9 slide 5

6 Bubble Sort Example Array numlist3 contains 17 23 5 11
Compare values 17 and 23. In correct order, so no exchange. Compare values 23 and 11. Not in correct order, so exchange them. 17 23 5 11 5. Not in correct order, Chapter 9 slide 6

7 Bubble Sort Example (continued)
After first pass, array numlist3 contains In order from previous pass Compare values 17 and 5. Not in correct order, so exchange them. 23. In correct order, so no exchange. 17 5 11 23 11. Not in correct order, Chapter 9 slide 7

8 Bubble Sort Example (continued)
After second pass, array numlist3 contains Compare values 5 and 11. In correct order, so no exchange. Compare values 17 and 23. In correct order, so 5 11 17 23 Compare values 11 and 17. In correct order, so In order from previous passes No exchanges, so array is in order Chapter 9 slide 8

9 Bubble Sort Tradeoffs Benefit Disadvantage
Easy to understand and implement Disadvantage Inefficiency make it slow for large arrays Chapter 9 slide 9

10 Selection Sort Algorithm
Locate smallest element in array and exchange it with element in position 0. Locate next smallest element in array and exchange it with element in position 1. Continue until all elements are in order. Chapter 9 slide 10

11 Selection Sort Example
Array numlist contains Smallest element is 2. Exchange 2 with element in 1st array position (i.e. element 0). 11 2 29 3 Now in order 2 11 29 3 Chapter 9 slide 11

12 Selection Sort – Example (continued)
Next smallest element is 3. Exchange 3 with element in 2nd array position. Next smallest element is 11. Exchange 11 with element in 3rd array position. Now in order 2 3 29 11 See pr9-05.cpp Now in order 2 3 11 29 Chapter 9 slide 12

13 //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]); Chapter 9 slide 13

14 Analysis of Sort Efficiency or Time Complexity
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) Chapter 9 slide 14

15 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 Chapter 9 slide 15

16 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

17 Example initial start end 1st pass start end 2nd pass sorted unsorted Chapter 9 slide 17

18 Example start end 4th pass sorted unsorted start end 3rd pass Chapter 9 slide 18

19 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

20 Best Case Analysis- Data Ordered
Pass # comparisons … N = (N-1) * 1 = N-1 = O(N) Chapter 9 slide 20

21 Worst Case Analysis Pass # comparisons … N-1 N (N-2) + (N-1) = O (N2) Chapter 9 slide 21

22 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


Download ppt "Alternate Version of STARTING OUT WITH C++ 4th Edition"

Similar presentations


Ads by Google