Download presentation

Presentation is loading. Please wait.

1
Searching and Sorting

2
Overview Search Analysis of search algorithms Sorting Analysis of sort algorithms Recursion p. 2 of 26

3
Linear Search Consider the code on p. 212 of your text. Explain the first line of the method. What does each symbol mean? Step by step, what would happen when this code searches an array of 100 items? What could we do to make this a non-static method? Searching and sorting p. 3 of 26

4
Linear search: analysis The time complexity of linear search is: A. O(1) B. O(log 2 n) C. O(n) D. O(2 n ) E. None of the above Searching and sorting p. 4 of 26

5
Binary search Consider the code on p. 215 of your text. Walk through the first line of the method. What does each symbol mean? What happens if there’s an odd number of items in the list being searched? What could we do to make this a non-static method? Searching and sorting p. 5 of 26

6
Binary Search: example What items would be considered (in order) if we were using binary search to locate the number 86 in the following list? 1 3 5 7 8 10 15 20 27 31 37 41 43 47 53 59 61 67 71 73 86 Searching and sorting p. 6 of 26

7
Binary search: analysis The time complexity of binary search is: A. O(1) B. O(log 2 n) C. O(n) D. O(2 n ) E. None of the above Searching and sorting p. 7 of 26

8
Sorting: selection sort public void selectionSort(){ //NOT static int min; T temp; for (int i=0;i<data.length-1;i++){ min=i; for (int j=i+1;j<data.length;j++){ if(data[j].compareTo(data[min])<0){ min=j; } temp=data[min]; data[min]=data[i]; data[i]=temp; } } Searching and sorting p. 8 of 26

9
Tracing selection sort Trace how selection sort would sort the following list: 4 1 3 2 Searching and sorting p. 9 of 26 Just before the value of i isThe list contains (in order) 04 1 3 2 1 2 3 4

10
Tracing selection sort (2) Trace how selection sort would sort the following list: 4 1 3 2 Searching and sorting p. 10 of 26 Just before the value of i isThe list contains (in order) 04 1 3 2 11 4 2 3 21 2 4 3 31 2 3 4 4No change – fails loop test

11
Selection sort: analysis The time complexity of selection sort is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. None of the above Searching and sorting p. 11 of 26

12
Tracing insertion sort Consider the code p. 223 (section 8.2) Trace how insertion sort would sort the following list: 4 1 3 2 Searching and sorting p. 12 of 26 Just before the value of i isThe list contains (in order) 04 1 3 2 1 2 3 4

13
Tracing insertion sort (2) Trace how insertion sort would sort the following list: 4 1 3 2 Searching and sorting p. 13 of 26 Just before the value of i isThe list contains (in order) 04 1 3 2 1 21 4 3 2 31 3 4 2 41 2 3 4 5No change – fails loop test

14
Insertion sort: analysis The time complexity of insertion sort is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. None of the above Searching and sorting p. 14 of 26

15
Sorting: quicksort Consider the code p. 227 (still section 8.2). You try: Sort the list 10 1 9 2 8 3 7 4 6 5 using quicksort. Trace the sort, and also draw a diagram showing the recursive calls. Searching and sorting p. 15 of 26

16
Characteristics of sorting algorithms Big-Oh Recursive? Stable? Searching and sorting p. 16 of 26

17
Quicksort: analysis The time complexity of quicksort is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. None of the above Searching and sorting p. 17 of 26

18
Sorting: mergesort Consider the code p. 230 (still section 8.2). Hand-trace sorting the list 10 1 9 2 8 3 7 4 6 5 using mergesort. Trace the sort, and also draw a diagram showing the recursive calls. Searching and sorting p. 18 of 26

19
Mergesort: analysis The time complexity of mergesort is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. None of the above Searching and sorting p. 19 of 26

20
Sorting: radix sort Consider the code pp. 233-4 (Section 8.3). Try sorting the list: 455 503 312 101 201 305 325 150 423 221 using radix sort. Trace the sort, and also draw a diagram showing the queues involved (similar to Figure 8.7). Searching and sorting p. 20 of 26

21
Radix sort: analysis The time complexity of radix sort is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. None of the above Searching and sorting p. 21 of 26

22
Time complexity of sorting Searching and sorting p. 22 of 26 AlgorithmTime complexity Selection sortO(n 2 ) Insertion sortO(n 2 ) QuicksortO(n 2 ), but O(n log n) on average MergesortO(n log n) Radix sortO(kn), where k is the # digits

23
Now see them run For animations of several common sorting algorithms, see http://www.sorting-algorithms.com Searching and sorting p. 23 of 26

24
Some built-in Java sorts For information about how this is done in Java: http://download.oracle.com/javase/tutorial/collections/alg orithms/index.html#sorting Java uses a “stable” sort – what does that mean? Give an example. Searching and sorting p. 24 of 26

25
Searching and sorting summary Know the algorithms, their complexity, and some pros and cons of: Linear vs. binary search Insertion sort, selection sort, quicksort, mergesort, radix sort With regard to ease of coding, time requirements, space requirements Searching and sorting p. 25 of 26

26
Coming attractions Next time, we’ll begin looking at a new data structure, trees, and a new Abstract Data Type, Dictionary. Homework: read chapter 9 (or the equivalent in the earlier edition). Searching and sorting p. 26 of 26

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google