DOC PREVIEW
MIT 6 006 - Sorting I- Heaps

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MIT OpenCourseWare http://ocw.mit.edu6.006 Introduction to AlgorithmsSpring 2008For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.Lecture 8 Sorting I: Heaps 6.006 Spring 2008 Lecture 8: Sorting I: Heaps Lecture Overview • Review: Insertion Sort and Merge Sort Selection Sort • • Heaps Readings CLRS 2.1, 2.2, 2.3, 6.1, 6.2, 6.3 and 6.4 Sorting Review Insertion Sort 524613524613213456425613425613214563keyθ(n2) algorithmFigure 1: Insertion Sort Example Merge Sort Divide n-element array into two subarrays of n/2 elements each. Recursively sort sub-arrays using mergesort. Merge two sorted subarrays. 1Lecture 8 Sorting I: Heaps 6.006 Spring 2008 24572 3611223 456 7LARθ(n) timeθ(n) auxiliary space24572 361A[1: n/2] A[n/2+1: n] want sorted A[1: n]w/o auxiliary space?? Figure 2: Merge Sort Example In-Place Sorting Numbers re-arranged in the array A with at most a constant number of them sorted outside the array at any time. Insertion Sort: stores key outside array Θ(n2) in-place Merge Sort: Need O(n) auxiliary space Θ(n lg n) during merging Question: Can we have Θ(n lg n) in-place sorting? Selection Sort 0. i = 1 1. Find minimum value in list beginning with i 2. Swap it with the value in ith position 3. i = i + 1, stop if i = n Iterate steps 0-3 n times. Step 1 takes O(n) time. Can we improve to O(lg n)? 2Lecture 8 Sorting I: Heaps 6.006 Spring 2008 21 54215421542145i = 1θ(n2) time in-placeFigure 3: Selection Sort Example Heaps (Not garbage collected storage) A heap is an array object that is viewed as a nearly complete binary tree. 16148 793 241101 2 3 4 5 6 7 8 9 101016148712534937624981Figure 4: Binary Heap Data Structure root A[i] Node with index i PARENT(i) = �2 i � LEFT(i) = 2i RIGHT(i) = 2i + 1 Note: NO POINTERS! 3�Lecture 8 Sorting I: Heaps 6.006 Spring 2008 length[A]: number of elements in the array heap-size[A]: number of elements in the heap stored within array A heap-size[A]: ≤ length[A] Max-Heaps and Min-Heaps Max-Heap Property: For every node i other than the root A[PARENT(i)] ≥ A[i] Height of a binary heap O(lg n) MAX HEAPIFY: O(lg n) maintains max-heap property BUILD MAX HEAP: O(n) produces max-heap from unordered input array HEAP SORT: O(n lg n) Heap operations insert, extract max etc O(lg n). Max Heapify(A,i) l left(i)← r right(i)← if l ≤ heap-size(A) and A[l] > A[i] then large st l← else largest i← if r ≤ heap-size(A) and A[r] > largest then large st r← if largest = i then exchange A[i] and A[largest] MAX HEAPIFY(A, largest) This assumes that the trees rooted at left(i) and Right(i) are max-heaps. A[i] may be smaller than children violating max-heap property. Let the A[i] value “float down” so subtree rooted at index i becomes a max-heap. 4Lecture 8 Sorting I: Heaps 6.006 Spring 2008 Example 101641471253493762898110161447125349376298110161487125349376249811081010MAX_HEAPIFY (A,2)heap_size[A] = 10Exchange A[2] with A[4]Call MAX_HEAPIFY(A,4) because max_heap property is violatedExchange A[4] with A[9]No more callsFigure 5: MAX HEAPIFY Example


View Full Document

MIT 6 006 - Sorting I- Heaps

Documents in this Course
Quiz 1

Quiz 1

7 pages

Quiz 2

Quiz 2

12 pages

Quiz 2

Quiz 2

9 pages

Quiz 1

Quiz 1

10 pages

Quiz 2

Quiz 2

11 pages

Quiz 1

Quiz 1

12 pages

Graphs

Graphs

27 pages

Load more
Download Sorting I- Heaps
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Sorting I- Heaps and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sorting I- Heaps 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?