Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS187 - Spring 2008 Lecture 19 Searching and Sorting Announcements - Program 5 now due Monday!! Sorting is chapter 10 in textbook.

Similar presentations


Presentation on theme: "CS187 - Spring 2008 Lecture 19 Searching and Sorting Announcements - Program 5 now due Monday!! Sorting is chapter 10 in textbook."— Presentation transcript:

1 CS187 - Spring 2008 Lecture 19 Searching and Sorting Announcements - Program 5 now due Monday!! Sorting is chapter 10 in textbook

2 Searching and Sorting Among the most important computing applications – We search and sort all the time: $, amts of things, alphabetizing, telephone number lookup, card playing,etc. Remember that one theme in the OO framework is codification, which goes like this: develop effective machinery for a general class or algorithm, wrap in a class, put it in a library. We’ll mostly look at comparison-based searching, sorting -- that is, we’ll use comparisons (and swapping) as primitives. Comparison count will generally be complexity measure.

3 Remember that within the library framework of Java, sorting is a kind of primitive method: Arrays.sort(theArray); Arrays.sort(theArray,widthComparatorObj); And Collections.sort(theList); Collections.sort(theList,widthComparatorObj);

4 Searching - here we’ll mostly talk about what’s been codified, and we’ll look at binary search (for an elementary account, see online iJava text, 8.1) So suppose you’re given an array (or arrayList, or LinkedList) that’s already in sorted order: int[] nums = {5,3,9,6,2}; // make an array of ints Arrays.sort(nums); // sort them Now let’s find the position of 6 in the order: System.out.println(Arrays.binarySearch(nums,6)); Ans: 3 System.out.println(Arrays.binarySearch(nums,7)); Ans: -5

5 Summary: Arrays (in java.util) a codification class: you get sort, binarySearch, also shuffle, and so forth. Collections (in java.util) does same thing for Collections classes, such as LinkedList, ArrayList

6 Some very simple sorts (we’ll work with ints) 12 9 33 41 5 18 15 Selection sort 129 33 41 5 18 15find the smallest 59 33 41 12 18 15swap with first element 5 | 9 33 41 12 18 15find next smallest 5 | 9 33 41 12 18 15swap with second element 59 | 33 41 12 18 15 find next smallest 69 12 | 41 33 18 15 And so forth Complexity (for an array - say - of length N)?

7 12 9 33 41 5 18 15 insertion sort (card-player’s sort) 12 Now insert the 9 “around” the 12 9 12 Now insert 33 into group 9 12 33 And so forth -- actually a bit more complicated:

8 12 | 9 33 41 5 18 15 Now insert 9 to right of bar: 9 12 | 33 41 5 18 15 And so forth Complexity (for an array - say - of length N)? What’s the behavior if array is all or mostly already sorted??

9 Shellsort Let’s look again at insertion sort It does very well if the items to be sorted are mostly in order. Shellsort takes advantage of this by sorting (progressively denser) subsequences of an array of elements

10 Shellsort for a 5-3-1 gap pattern 81 94 11 96 12 35 17 95 28 58 41 75 15 ------------------------------------------------ After sorting the offset 5 entries: 35 17 11 28 12 41 75 15 96 58 81 94 95 After sorting the offset 3 entries: 28 12 11 35 15 41 58 17 94 75 81 96 95 After sorting the offset 1 entries: 11 12 15 17 28 35 41 58 75 81 94 95 96

11 What’s a good ShellSort pattern? Answers mostly empirical / people did vast performance studies in the late 50’s,60’s, 07’s Properties, Proofs 1)From gap-cycle to gap-cycle, subsequence sorted orders are preserved.. 2)For this sequence: 1 4 13 40 121 364.. ShellSort does fewer than O(N 3/2 ) comparisons 3)This sequence gets you to O(N 4/3 ): 1, 8, 23, 77,… 4) Other sequences do even better: O(N (logN) 2 )

12 MergeSort - a true O(NlogN) sort Simple, recursive: Base: for 1, 2 element sequences, do the obvious; Recur: given 2 ~equal-sized sorted subsequences, merge them into a sorted sequence Biggest drawback: the merge requires extra space (O(N))

13 Heapsort - based on heaps Also an O(N log N) sort

14 Creating a Heap Fig. 27-7 The steps in adding 20, 40, 30, 10, 90, and 70 to a heap.

15 Creating a Heap Fig. 27-8 The steps in creating a heap by using reheap. More efficient to use reheap than to use add

16 Heapsort Possible to use a heap to sort an array Place array items into a maxheap Then remove them –Items will be in descending order –Place them back into the original array and they will be in order -- Note: reheap == downheap, or swim

17 Heapsort Fig. 27-9 A trace of heapsort (a – c)

18 Heapsort Fig. 27-9 A trace of heapsort (d – f)

19 Heapsort Fig. 27-9 A trace of heapsort (g – i)

20 Heapsort Fig. 27-9 A trace of heapsort (j – l)

21 Heapsort Implementation of heapsort public static void heapSort(Comparable[] array, int n) {// create first heap for (int index = n/2; index >= 0; index--) reheap(array, index, n-1); swap(array, 0, n-1); for (int last = n-2; last > 0; last--) {reheap(array, 0, last); swap(array, 0, last); } // end for } // end heapSor private static void reheap(Comparable[] heap, int first, int last) {... } // end reheap Efficiency is O(n log n). However, quicksort is usually a better choice. Efficiency is O(n log n). However, quicksort is usually a better choice.

22 A special Selection sort Consider: 5 3 8 9 1 6 74 2 = 5 3 8 9 1 6 7 4 2 First find min in each block Now: 1) find min of mins 2) return this min value 3) update contributing block repeat

23 Quick reminder: If you create a Jbutton Jbutton myButton = new Jbutton(“me”); You can change the inscription on the button at any time with this inscription: myButton.setText(“you”);


Download ppt "CS187 - Spring 2008 Lecture 19 Searching and Sorting Announcements - Program 5 now due Monday!! Sorting is chapter 10 in textbook."

Similar presentations


Ads by Google