* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Selection sort
Survey
Document related concepts
Transcript
Selection sort To understand heap sort, let's start with selection sort. An experiment: I write a list of numbers, once I’m done you tell me sorted order. 5,2,100,19,22,7 How did you go about ordering them? You probably looked through the list for the first number, then looked through it again for the next one, etc. One way of formalizing this process is called selection sort: selection sort(list L) list X = empty while L nonempty remove smallest element of L and add it to X Time analysis: 1. there is one loop, executed n times. 2. The total time is not 0(n). Remember we are counting comparisons. "Remove the smallest element of L" could take many comparisons. Need to look more carefully at this part of the loop. (The other part, adding an element to X, also depends on how we store X, but can be done in constant time for most reasonable implementations and in any case doesn't require any comparisons, which is what we re counting.) 3. The obvious method of finding (and removing) the smallest element: scan L and keep track of the smallest object. So this produces a nested inner loop, time = O(length of L) so total time = O(sum i) = O(n 2). 4. This is one of the slow algorithms. In fact it is as slow as possible: it always makes every possible comparison. Why bother describing it if there are so many better algorithms? Heap sort Heap sort (invented by J.R.J. Williams) looks exactly like the pseudo-code above for selection sort, and simply uses some data structures to perform the main step of selection sort more quickly. The operations performed in Heapsort are : Start with a list L and turn it into a copy of whatever data structure we're using, Find the smallest object in the data structure, and Remove the smallest element 1. There are many suitable data structures. We will use a structure called a binary heap. 2. A heap also supports other possible operations, such as adding objects to the list; that's not useful in this algorithm but maybe later. 3. We might see heaps again when we talk about minimum spanning trees and shortest paths. Simple analysis of heap sort: 1. if we can build a data structure from our list in time X and 2. if finding and removing the smallest object takes time Y then the total time will be O(X + nY). 3. In our case X will be 0(n) and Y will be O(log n) so total time will be O(n + n log n) = O(n log n) Heap data structure We form a binary tree with certain properties: The elements of L are placed on the nodes of the tree; each node holds one element and each element is placed on one node. The tree is balanced which, as far as this discussion is concerned, means that all paths have length O(log n). (The heap property): If one node is a parent of another, the value at the parent is always larger than the value at the child. You can think of the heap property as being similar to a property of family trees -- a parent's birthday is always earlier than his or her childrens' birthdays. You can find the largest heap element by looking at root of the tree ; this is easy to see, since any node in a tree has a larger value than all its descendants (by transitivity). How should we remove it? Say the company boss quits. How do we fill his place? We have promote somebody. To satisfy the heap property, that will have to be the person with the biggest salary, but that must be one of his two direct underlings (the one of the two with the bigger salary). Promoting this person then leaves a vacancy lower down that can be filed in the same way. Number of comparison steps in such an operation ? = length of the longest path in the tree = O ( n log n ) So the Number of comparisons in Heapsort = O ( n log n ) + work it takes to set up the heap Basic Idea ( pseudocode ) for Heapsort heapsort ( List L ) make heap H from L make empty list X while H is nonempty remove largest element from H and add to X return X What’s left to do ? Make the Heap ? - i.e. construct a balanced binary tree with the heap property from a given input list, L. 1. Can construct a binary tree from the list L ? 2. Now, swap elements of this tree around so that the heap property is satisfied --- enter the Heapify routine . Heapify ( tree T ) heapify ( left subtree) heapify ( right subtree) let x = value of tree root while node containing x does not satisfy heap property switch values of node and its largest child Notice : the while loop requires 2 comparisons per iteration AND it takes at most log n iterations ( Why ? ) so T ( n ) < 2 T ( n/2 ) + 2 log n , is the time to perform the heapify operation and the solution is T ( n ) < 4 n. ==> Heapsort requires no more than n log n + 4 n comparisons.