SlideServe
Menu
Préférences
© SlideServe · v2026

C++ Programming

kieve avatar
25 diapositives · 481 Views · Jul 29, 2014

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

C++ Programming - slide 1
01 / 25C++ Programming
1 / 17

Télécharger la présentation

C++ Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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 } }