1 / 25

C++ Programming

C++ Programming. Jerry Lebowitz. Chapter 16. Topics. Sorting Searching. Why Study Sorting? . Sorting is a classic subject in computer science There are three reasons for studying sorting algorithms

kieve
Download Presentation

C++ Programming

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. C++ Programming Jerry Lebowitz

  2. Chapter 16

  3. Topics • Sorting • Searching

  4. Why Study Sorting? • Sorting is a classic subject in computer science • There are three reasons for studying sorting algorithms • First, sorting algorithms illustrate many creative approaches to problem solving and these approaches can be applied to solve other problems • Second, sorting algorithms are good for practicing fundamental programming techniques using selection statements, loops, methods, and arrays • Third, sorting algorithms are excellent examples to demonstrate algorithm performance

  5. Bubble Sort Algorithm (1) • The bubble sort algorithm makes several passes through an array • On each pass, successive neighboring pairs are compared • If a pair is in decreasing order, its values are swapped • The smaller values gradually “bubble” up to the top

  6. Bubble Sort Algorithm (2) • After the first pass, the last element becomes the largest in the array • After the second pass, the second to last element becomes the second largest in the array • The process continues until all the elements are sorted

  7. Bubble Sort See bubble sort example

  8. Bubble Sort Complexity • In the best case, the bubble sort algorithm just take one pass • Run time would be O(n) since there are n elements in the array • In the worst case, the bubble sort requires n – 1 passes • The first pass takes n – 1 comparisons • The second pass takes n – 2 comparisons • And so on • The total number of comparisons is

  9. Insertion Sort • The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted

  10. Insertion Sort

  11. Insertion Sort int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted

  12. How to Insert? The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted See insertion code

  13. Selection Sort • Selection sort finds the largest number in the list and places it last • It then finds the largest number remaining and places it next to last, and so on until the list contains only a single number

  14. Selection Sort

  15. Selection Sort int[ ] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted

  16. Selection Sort Code /** The method for sorting the numbers */ void selectionSort(double[] list) { for (int i = list.length - 1; i >= 1; i--) { // Find the maximum in the list[0..i] double currentMax = list[0]; intcurrentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } } // Swap list[i] with list[currentMaxIndex] if necessary; if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; list[i] = currentMax; } } }

  17. Binary Search (1) • For binary search to work, the elements in the array must already be ordered • Without loss of generality, assume that the array is in ascending order e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79 • The binary search first compares the key with the element in the middle of the array

  18. Binary Search (2) Consider the following three cases: • If the key is less than the middle element, one only needs to search the key in the first half of the array • If the key is equal to the middle element, the search ends with a match • If the key is greater than the middle element, one only needs to search the key in the second half of the array

  19. Determine if 75 is in the List • Compare 75 with the middle element in the list, list[5] (which is 39). • Because 75  list[5] and 75 > list[5], we then restrict our search to the list list[6]...list[11]

  20. Binary Search Key List 8 8 8

  21. Another Binary Search

  22. Still Another Binary Search

  23. Binary Search Results • The binarySearch method returns the index of the element in the list that matches the search key if it is contained in the list • Otherwise, it returns – 1

  24. Binary Search Code intbinarySearch(const int list[], intlistLength, intsearchItem) { int first = 0; int last = listLength - 1; int mid; bool found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid] == searchItem) found = true; else if(list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if(found) return mid; else return –1; }//end binarySearch (see example bsearch.cpp)

  25. Binary Search Algorithm int binarySearch(int[] list, int key) length) { int low = 0; int high = length - 1; while (high >= low) { // int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return -low - 1; // Now high < low } }

More Related