22
HEAPSORT COUNTING SORT RADIX SORT

HEAPSORT COUNTING SORT RADIX SORT. HEAPSORT O(nlgn) worst case like Merge sort. Like Insertion Sort, but unlike Merge Sort, Heapsort sorts in place: Combines

Embed Size (px)

Citation preview

HEAPSORTCOUNTING SORT

RADIX SORT

2

HEAPSORT

• O(nlgn) worst case like Merge sort.• Like Insertion Sort, but unlike Merge Sort,

Heapsort sorts in place:• Combines the best of both algorithms.• To understand heapsort, we’ll cover heaps and

heap operations.

3

Heap data structure

• The (binary) heap data structure is an array object that we can view as a nearly complete binary tree

• Height of node = # of edges on a longest simple path from the node down to a leaf.• Height of heap = height of root = (lgn).• A heap can be stored as an array A.

• Root of tree is A[1].• Parent of A[i ] = A[i/2].• Left child of A[i ] = A[2i ].• Right child of A[i ] = A[2i + 1].

A.length, number of elements in the array,A.heap-size,how many elements in the heap are stored within array A. 0 <=A.heap-size <=A.length

a binary tree is a tree data structure in which each node has at most two child nodes

4

Heap data structure

• There are two kinds of binary heaps: max-heaps and min-heaps. In both kinds, the values in the nodes satisfy a heap property, the specifics of which depend on the kind of heap.

• In a max-heap, the max-heap property is that for every node i other than the root, – A[PARENT(i)] >=A[İ]

• In a min-heap, the min-heap property is that for every node i other than the root, – A[PARENT(i)] <=A[İ]

• For the heapsort algorithm we use max-heaps.

5

Maintaining the heap property

• In order to maintain the max-heap property, we call the procedure MAX-HEAPIFY.

• Its inputs are an array A and an index i into the array.

6

7

Building a heap

• We can use the procedure MAX-HEAPIFY in a bottom-up manner to convert an array A[1…n], where n = A.length, into a max-heap.

8

ççç

9

Heapsort algorithm

• The HEAPSORT procedure takes time O(nlgn), since the call to BUILD-MAXHEAP takes time O(n) and each of the n -1 calls to MAX-HEAPIFY takes time O(lgn)

The complexity of BuildMaxHeap appears to be Θ(nlgn) **n calls to MaxHeapify at a cost of Θ(lgn) per call, but this result can be improved to Θ(n). The analysis is in the book. The intuition is that most of the calls to heapify are on very short heaps.

10

11

Counting Sort

• Counting sort assumes that each of the n input elements is an integer in the range 0 to k.

• Counting sort determines, for each input element x, the number of elements less than x.

• It uses this information to place element x directly into its position in the output array.

12

Counting Sort

13

Counting Sort

14

Radix Sort• Radix sort is the algorithm used by the card-sorting machines you now

find only in computer museums.• Radix sort solves the problem of card sorting by sorting on the least

significant digit first.• The algorithm then combines the cards into a single deck, with the cards

in the 0 bin preceding the cards in the 1 bin preceding the cards in the 2 bin, and so on. Then it sorts the entire deck again on the second-least significant digit and recombines the deck in a like manner.

• The process continues until the cards have been sorted on all d digits. Remarkably, at that point the cards are fully sorted on the d-digit number.

15

Radix Sort• In a typical computer, which is a sequential random-access machine, we

sometimes use radix sort to sort records of information that are keyed by multiple fields.

• For example, we might wish to sort dates by three keys: year, month, and day. We could run a sorting algorithm with a comparison function that, given two dates, – compares years, and if there is a tie, – compares months, and if another tie occurs, – compares days.

• Alternatively, we could sort the information three times with a stable sort: first on day, next on month, and finally on year.

16

Radix Sort

• The code for radix sort is straightforward. • The following procedure assumes that each element in the n-element array A has d digits,

where digit 1 is the lowest-order digit and digit d is the highest-order digit.

http://www.youtube.com/watch?v=4S1L-pyQm7Y

17

Radix Sort

18

HATIRLATMALAR

19

20

21

Exercise 6.4.1

• llustrate the operation of HEAPSORT on the array

A ={5, 13, 2, 25, 7, 17, 20, 8, 4}.