cs187 - spring 2008 lecture 19 searching and sorting announcements - program 5 now due monday!!...

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

Upload: darren-grant

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

CS187 - Spring 2008

Lecture 19

Searching and Sorting

Announcements -

•Program 5 now due Monday!!

•Sorting is chapter 10 in textbook

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.

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

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

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

Some very simple sorts (we’ll work with ints)

12 9 33 41 5 18 15

Selection sort

129 33 41 5 18 15 find the smallest

139 33 41 12 18 15swap with first element

5 | 9 33 41 12 18 15 find next smallest

5 | 9 33 41 12 18 15 swap with second element

5 9 | 33 41 12 18 15 find next smallest

6 9 12 | 41 33 18 15

And so forth

Complexity (for an array - say - of length N)?

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:

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

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

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:

81 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

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(N3/2) comparisons

3) This sequence gets you to O(N4/3): 1, 8, 23, 77,…

4) Other sequences do even better: O(N (logN)2)

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

Heapsort - based on heaps

Also an O(N log N) sort

Creating a Heap

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

Creating a Heap

Fig. 27-8 The steps in creating a heap by using reheap.

More efficient to use reheap than to

use add

More efficient to use reheap than to

use add

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

Heapsort

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

Heapsort

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

Heapsort

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

Heapsort

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

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){ < Statements from Segment 27.11, replacing rootIndex with first and

lastIndex with 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.

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

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