# Chapter 9 Sorting and Searching Arrays Starting Out

• Slides: 15

Chapter 9: Sorting and Searching Arrays Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

9. 1 The Bubble Sort Algorithm Bubble sort is a simple sorting algorithm for rearranging the contents of an array – Useful for alphabetical lists and numerical sorting – Can be done in ascending or descending order – With the Bubble Sort, array elements are compared and numbers bubble toward the end of the array (assuming your sorting in ascending order) – Swapping elements must be done in order to properly sort the array Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2

9. 1 The Bubble Sort Algorithm Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3

9. 1 The Bubble Sort Algorithm Inside Program 9 -1 – max. Element variable holds the subscript of the last element that is to be compared to its neighbor – index variable is an array subscript in one loop – The outer loop iterates for each element in the array – The inner loop iterates for each of the unsorted array elements – The if statement does the comparison Sorting an array of strings to put information in alphabetical order can be done with a Bubble Sort Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4

9. 2 The Selection Sort Algorithm The selection sort works similar to the bubble sort, but more efficient – Bubble sort moves one element at a time – Selection sort performs fewer swaps because it moves items immediately to their final position Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5

9. 2 The Selection Sort Algorithm Figure 9 -17 Flowchart for the selection. Sort module Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6

9. 2 The Selection Sort Algorithm Inside Figure 9 -17 – min. Index holds the subscript of the element with the smallest value – min. Value holds the smallest value found – The outer loop iterates for each element in the array, except the last – The inner loop performs the scan Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7

9. 3 The Insertion Sort Algorithm The insertion sort algorithm sorts the first two elements, which becomes the sorted part of the array – Each remaining element is then inserted into the sorted part of the array at the correct location – Also more efficient than the bubble sort Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8

9. 3 The Insertion Sort Algorithm Figure 9 -24 Flowchart for the insertion. Sort module Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9

9. 3 The Insertion Sort Algorithm Inside Figure 9 -24 – scan is used to scan through the array – unsorted. Value holds the first unsorted value – The outer loop steps the index variable through each subscript, starting at 1 – The inner loop moves the first element outside the sorted subset and into its proper position Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10

The Swap Module A closer look at the swap module – In most of the sorts, a swap module can be called – It is the same in each sorting algorithm and only changes in the parameter list to account for the type of data passed to it //This swap module accepts two Integer arguments Module swap(Integer Ref a, Integer Ref b) Declare Integer temp //swap the values Set temp =a Set a = b Set b = temp End Module Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11

9. 4 The Binary Search Algorithm The binary search algorithm locates an item in an array by repeatedly dividing the array in half – Each time it divides the array, it eliminates the half of the array that does not contain the item – It’s more sequential than the selection search because each time it cuts the array in half and makes a smaller number of comparisons to find a match Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12

9. 4 The Binary Search Algorithm How it works – Requires that the array is first sorted – The first comparison is done with the middle element of the array to see if it is greater than or less than the number that is being searched – If it’s greater than, then the number must be in the first half of the array – If it’s less than, then the number must be in the second half of the array – This process is continued until the match if found Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13

9. 4 The Binary Search Algorithm Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14

9. 4 The Binary Search Algorithm Inside Program 9 -17 – middle is used to store the calculated middle index – value stores the value being searched for – A loop hold the search and iterates as long as there are elements or until a match is found – Nested if-then-else's perform the comparisons Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15