260 likes | 440 Views
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
E N D
C++ Programming Jerry Lebowitz
Topics • Sorting • Searching
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
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
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
Bubble Sort See bubble sort example
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
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
Insertion Sort int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
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
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
Selection Sort int[ ] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
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; } } }
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
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
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]
Binary Search Key List 8 8 8
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
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)
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 } }