Download presentation

Presentation is loading. Please wait.

Published byKeanu Lowther Modified over 3 years ago

1
CMSC 2021 Searching and Sorting

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

3
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

4
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

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

6
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?

7
CMSC 2027 Sorting Bubble Sort Selection Sort Merge Sort Quicksort

8
CMSC 2028 CPGATOBCPGATOB Bubble Sort

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

10
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; }

11
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++; }

12
CMSC 20212 CPGATOBCPGATOB Selection Sort

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

14
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 ; }

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

16
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)

17
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) ; }

18
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) ;

19
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

20
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

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

22
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)

23
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) ; }

24
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);

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

Similar presentations

OK

Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 11-2 Chapter Objectives Examine the linear search and.

Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 11-2 Chapter Objectives Examine the linear search and.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on polynomials download yahoo File type ppt on cybercrime convention Ppt on mobile communication and bluetooth Ppt on mauryan dynasty Ppt on network security algorithms Ppt on art of living download Ppt on masculine and feminine gender for grade 3 Maths ppt on real numbers for class 10th Ppt on electricity for class 10th sample Topics for ppt on environmental management