Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.

Similar presentations


Presentation on theme: "Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap."— Presentation transcript:

1 Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap

2 Quicksort Reminder: Quicksort utilizes the divide-and-conquer approach by recursively splitting the array with a chosen pivot into two subarrays, one of values smaller than the pivot and the other of values greater than the pivot. 36812574 312 4 8576 1 2 345 6 78 1234567 8 pivot Here the pivots were chosen to be the last element in each subarray

3 Quicksort With a good choice of pivots, the sort can run in O(nlogn) time with relatively small constant factors. A randomized version of Quicksort: the pivot is chosen randomly. The Randomized-Quicksort has an expected running time of O(nlogn).

4 Quicksort - Median-of-3 Partition Quicksort can be improved by a better selection of pivots - instead of choosing a pivot randomly, select 3 random elements and choose the pivot to be the median of the three. There is a greater chance of choosing a “good” pivot. The expected running time is still O(nlogn), but the constant factor is smaller.

5 Average Running Time Quicksort with deterministic choice of pivot runs in O(nlogn) time in the average case. This means that the expected running time over all possible inputs of size n is O(nlogn). “Good” inputs run in O(nlogn) time, while “bad” inputs may run in O(n 2 ). For example, if the input is already sorted, then it is a “bad” input.

6 Randomized Randomized-Quicksort chooses the pivot randomly. Here, for every possible input, the average running time of all possible runs with this input is O(nlogn). There are no “good” or “bad” inputs, every input can sometimes run in O(nlogn) and sometimes in O(n 2 ).

7 Bucket Sort Assumption: The input – n real numbers in the interval [0,1) (i.e. ) – distributed uniformly. The algorithm: divide the interval into n ‘buckets’ and then distribute the numbers into the buckets. Each bucket is sorted with insertion sort and then the buckets are concatenated into a single sorted list. 01

8 Bucket Sort Since the data is uniformly distributed, the number of elements in each bucket would be small. It can be shown that the expected number of elements in each bucket would be O(1). Total average running time is O(n).

9 Heaps A heap is a complete binary tree, in which each node is larger than both its sons. The largest element is in the root. 16 13 123 521 Note: this does not mean that the root’s 2 sons are the next largest. 9 74

10 Heaps A heap can be represented by an array. Levels are stored one after the other. The root is stored in A[1]. The sons of A[i] are A[2i] and A[2i+1]. 16 13 123 521 9 74 1613912374521

11 Heapify Assumes that both subtrees of the root are heaps, but the root may be smaller than one of its children. 1 13 123 52 9 74 The idea is to let the value at the root to “float down” to the right position.

12 Heapify 13 12 53 12 9 74 Heapify(Node x) largest = max {left(x), right(x)} if ( largest > x ) exchange (largest, x) heapify (x) 1 13 123 52 9 74

13 Heap-Extract-Max 16 13 123 521 9 74 Save the root as max. Remove the last node and place it in the root. Do Heapify. Return max. 13 12 53 12 9 74

14 Heap-Insert 16 13 123 521 9 74 Insert new value at the end of the heap. Let it “float up” to the right position. 15 16 15 1213 521 9 74 3 new value

15 Priority Queue Each inserted element has a priority. Extraction order is according to priority. Supported operation are Insert, Maximum, Extract-Max. Easily implemented with heaps.

16 Priority Queue Priority Queues using heaps: –Maximum operation takes O(1) –Extract-Max operation takes O(logn) –Insert operation takes O(logn) Priority Queues using sorted list –Maximum operation takes O(1) –Extract-Max operation takes O(1) –Insert operation takes O(n)

17 Build-Heap Build-Heap(A) for i =  length[A]/2  downto 1 do Heapify[A,i] 3572 1 94121613 357 2 9412161 35 7 13941221 3 5 91613741221 3 169121374521 1613912374521

18 Heapsort Heapsort(A) Build-Heap(A) for i=length[A] downto 2 do exchange A[1] with A[i] heap-size[A]=heap-size[A]-1 Heapify(A, 1) 16 1391237452 1 13 12953741 2 16 12 592374 1 1316 9 57231 4 121316 7 5423 1 9121316 5 342 1 79121316

19 Build-Heap vs. Heap-Insert Build-Heap runs in O(n). Inserting n items takes O(nlogn). Sometimes Build-Heap and Heap-Insert create different heaps from the same input. –For example: the input sequence 1, 2, 3, 4 4 23 1 4 32 1 Build-Heap:Heap-Insert:

20 Questions How to implement a stack/queue using a priority queue? How to implement an Increase-Key operation which increases the value of some node? How to delete a given node from the heap in O(logn) ? How to search for a key in a heap?


Download ppt "Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap."

Similar presentations


Ads by Google