This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS61B Summer 2006 Instructor: Erin Korber Lectures 13,14,15: 18,19,20 July Sorting The need to sort numbers, strings, and other records arises frequently in computer applications. The entries in any modern phone book were sorted by a computer. Databases have features that sort the records returned by a query, ordered according to any field the user desires. Sorting is perhaps the simplest fundamental problem that offers a large variety of algorithms, each with its own inherent advantages and disad vantages. We’ll study and compare several sorting algorithms, using the algorithm analysis tools that we’ve learned. Most of the sorting algorithms that we discuss (the first 5) will be “comparisonbased”  that is, they compare pairs of items to sort the set. A feature of the Java library that will be relevant in this context is the Comparable interface, which has one method, compareTo . This, as you might guess, compares two objects. So things that you want to sort using comparisonbased methods should implement this interface. 1 Insertion Sort Insertion sort is very simple and runs in O ( n 2 ) time. We employ a list S, and maintain the invariant that S is sorted. Pseudocode: Start with an empty list S and the unsorted list I of n input items. for (each item x in I) { insert x into the list S, positioned so that S remains in sorted order. } S may be an array or a linked list. If S is a linked list, then it takes Θ( n ) worstcase time to find the right position to insert each item (since we have to walk down the list to the right position to insert into). If S is an array, we can find the right position to insert into in O(log n) time by binary search, but it takes Θ( n ) worstcase time to shift the larger items over to make room for the new item. In either case, insertion sort is an O ( n 2 ) algorithm–but for a different reason in each case. 1 What are the memory requirements for this sort? Can you think of a way to do it with lists without creating a lot of new objects? If S is an array, one of the nice things about insertion sort is that it’s an inplace sort. An ”inplace” sort leaves the sorted items in the same array that initially held the input items, and uses only O(1) or perhaps O(log n) additional memory (in addition to the input array). To do an inplace insertion sort, we partition the array into two pieces: the left portion (initially empty) holds S, and the right portion holds I. With each iteration, the dividing line between S and I moves one step to the right. ][7395 => 7][395 => 37][95 => 379][5 => 3579][ \_____/ S \___/ \_/ \_/ \___/ I \_____/ I I S I S S If the input list I is ”almost” sorted, insertion sort can be as fast as Θ( n ), if the algorithm starts its search for the right place to insert from the end of S. In this case, the running time is proportional to n plus the number of “inversions”. An inversion is a pair of valuesof “inversions”....
View
Full Document
 Spring '10
 JenniferChen
 Sort

Click to edit the document details