1 / 15

Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

Chapter 9: Sorting and Searching Arrays. Starting Out with Programming Logic & Design Second Edition by Tony Gaddis. 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

mckeel
Download Presentation

Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9:Sorting and Searching Arrays Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

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

  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. 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. 9.2 The Selection Sort Algorithm Figure 9-17 Flowchart for the selectionSort module

  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. 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. 9.3 The Insertion Sort Algorithm Figure 9-24 Flowchart for the insertionSort module

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

  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

More Related