# CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search.

## Presentation on theme: "CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search."— Presentation transcript:

CMSC 2021 Searching and Sorting

CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search

CMSC 2023 Linear Search fig - 14 items cantalope - minimum comparisons = 1 pineapple - maximum comparisons = 13 cherry - on average, 13/2 = 6 to 7 comparisons kiwi lemon apple Not a very efficient search! So, do we watermelon never use it? lime melon orange pear banana tangerine

CMSC 2024 Linear Search on An Ordered List apple -14 items banana - minimum comparisons = 1 cantalope - maximum comparisons = 13 cherry - on average, 13/2 = 6 to 7 comparisons fig kiwi So why is a linear search more efficient lemon on an ordered list than an unordered list? lime melon So should we always choose to order a list orange before performing a linear search on it? pear pineapple tangerine watermelon

CMSC 2025 Binary Search apple banana cantalope cherry fig kiwi lemon lime melon orange pear pineapple tangerine watermelon

CMSC 2026 Binary Search (con’t) Pretty efficient! As the number of items doubles, the number of comparisons increases by only one! So, if it is so efficient, do we always choose to do a binary search?

CMSC 2027 Sorting Bubble Sort Selection Sort Merge Sort Quicksort

CMSC 2028 CPGATOBCPGATOB Bubble Sort

CMSC 2029 How many loops will the bubble sort algorithm have? What will the loop(s) do? Bubble Sort (con’t)

CMSC 20210 void bubbleSort1(data array[], int numElements) { int pass, i; data temp; for (pass = 1; pass <= (numElements-1); pass++) { for (i = 0; i <= (numElements-2); i++) { if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; }

CMSC 20211 void bubbleSort2(data array[], int numElements) { int pass, i, swapMade; // swap flag added data temp; pass = 1; swapMade = 1; // initialize to true while ((pass<=(numElements-1)) && (swapMade==1)) { swapMade = 0; // reset to false for (i = 0; i <= (numElements-2); i++) { if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; swapMade = 1; // set to true } pass++; }

CMSC 20212 CPGATOBCPGATOB Selection Sort

CMSC 20213 How many loops will the selection sort algorithm have? What will the loop(s) do? Selection Sort (con’t)

CMSC 20214 // From on-line notes void selection_sort(data A[], index low, index high) { index i, j, min ; data temp ; for (i = low ; i < high ; i++) { min = i ; for (j = i+1 ; j <= high ; j++) { if (A[j] < A[min]) min = j ; } temp = A[min] ; A[min] = A[i] ; A[i] = temp ; }

CMSC 20215 Merge Sort 40 20 10 80 60 50 7 30 100 90 70

CMSC 20216 Merge Sort is a “divide and conquer” algorithm The array is repeatedly divided in half, then the arrays are repeatedly merged What type of algorithm do you think would be appropriate? Merge Sort (con’t)

CMSC 20217 // From on-line notes void mergesort(data A[], index low, index high) { index mid ; if (low >= high) return ; mid = (low + high) / 2 ; mergesort(A, low, mid) ; mergesort(A, mid + 1, high) ; merge(A, low, mid, mid + 1, high) ; }

CMSC 20218 /* Global variable temp must be allocated the same amount of space as the array to be sorted */ data *temp ; void merge(data A[], index low1, index high1, index low2, index high2) { index t, i1, i2 ; /* Sanity check */ assert(low2 == high1 + 1) ;

CMSC 20219 /* while there are elements in both halves, copy the lowest one */ i1 = low1 ; i2 = low2 ; t = 0 ; while (i1 <= high1 && i2 <= high2) { if (A[i1] < A[i2]) { temp[t] = A[i1] ; i1++ ; t++ ; } else { temp[t] = A[i2] ; i2++ ; t++ ; } } // end while

CMSC 20220 /* copy any remaining elements */ while (i1 <= high1) { temp[t] = A[i1] ; t++ ; i1++ ; } while (i2 <= high2) { temp[t++] = A[i2++] ; } /* copy the now-sorted elements back into the original array */ for (t = low1; t <= high2; t++) { A[t] = temp[t - low1] ; } } // end function

CMSC 20221 40 20 10 80 60 50 7 30 100 90 70 Quicksort

CMSC 20222 Quicksort is also a “divide and conquer” algorithm The array is continually divided in half at the pivot point and rearranged Therefore, recursion is a wise choice Quicksort (con’t)

CMSC 20223 // From on-line notes void quicksort(data A[], index low, index high) { index q ; if (low >= high) return ; q = partition(A, low, high) ; assert(q < high) ; quicksort(A, low, q) ; quicksort(A, q + 1, high) ; }

CMSC 20224 index partition(data A[], index low, index high) { data x, temp ; index i, j ; i = low - 1; j = high + 1; x = A[low] ; while (1) { /* Find an element less than x */ do { j = j - 1; } while (A[j] > x) ; /* Find an element greater than x */ do { i = i + 1; } while (A[i] < x);

CMSC 20225 if (i < j) { temp = A[j] ; A[j] = A[i] ; A[i] = temp ; } else { return j; } } // end while } // end function

Download ppt "CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search."

Similar presentations