Download presentation

Presentation is loading. Please wait.

Published byLynne McDowell Modified over 2 years ago

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

2
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 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

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

4
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4 9.1 The Bubble Sort Algorithm Inside Program 9-1 –maxElement 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

5
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5 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

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

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

8
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8 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

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

10
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10 9.3 The Insertion Sort Algorithm Inside Figure 9-24 –scan is used to scan through the array –unsortedValue 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

11
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11 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

12
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12 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

13
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13 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

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

15
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15 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

Similar presentations

OK

Searches & Sorts V22.0002.001 Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.

Searches & Sorts V22.0002.001 Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on area of parallelogram and triangles in nature File type ppt online voting system Ppt on transportation and communication Ppt on data handling for class 2 Ppt on video teleconferencing training Ppt on producers consumers and decomposers in a food Ppt on annual sports day Ppt on electricity for class 10th sample Ppt on automobile related topics such Ppt on case study format