15
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

Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Embed Size (px)

DESCRIPTION

Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Citation preview

Page 1: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 2: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 3: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3

9.1 The Bubble Sort Algorithm

Page 4: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4

9.1 The Bubble Sort AlgorithmInside 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

Page 5: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 6: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 7: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 8: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 9: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 10: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 11: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11

The Swap ModuleA 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

Page 12: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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

Page 13: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13

9.4 The Binary Search AlgorithmHow 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

Page 14: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14

9.4 The Binary Search Algorithm

Page 15: Starting Out With Programming Logic & Design - Chapter9_Sorting and Searching Arrays

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