Quicksort - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Quicksort

Description:

A heap is a complete binary tree of height k where ... To create a heap within a node and its immediate descendents. Compare node i with its children. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 12
Provided by: me6122
Category:
Tags: heap | quicksort

less

Transcript and Presenter's Notes

Title: Quicksort


1
Quicksort
  • Extremely Fast Average Running Time (hence the
    name)
  • Worst Case Complexity O(n2) asymptotically
    sub-optimal
  • Divide and Conquer Approach
  • Partition data into smaller sets and work on each
    set independent of the other sets.
  • Choose a pivot element, move all data smaller
    than pivot to the left and all data larger than
    pivot to the right.
  • When pivot is in place, we have 2 partitions
  • Left partition with all elements smaller than
    pivot
  • Right partition with all elements larger than
    pivot
  • Recursively apply partition procedure to each
    sub-partition until partition size1.

2
Partitioning
  • Take first element as pivot
  • Define left (L) and right (R) pointers as
    pointing to second and last element respectively.
  • While (Pivot lt AR) and (RgtL)
  • R ? R-1
  • If (Pivot gt AR
  • Swap Pivot and AR
  • else if (LR) goto
  • While (Pivot gt AL) and (RgtL)
  • L ? L1
  • If (Pivot lt AL)
  • Swap Pivot and AL
  • goto 1
  • 5. Pivot is now in place, apply process
    recursively on left and right partitions until
    partition size1.

3
  • E.g. 3 5 4 8 1 6 2 7
  • 3 is the pivot element
  • L and R point to 5 and 7 respectively

swap
3
5
4
8
1
6
2
7
3
5
4
8
1
6
2
7
L
R
L
R
swap
3
5
4
8
1
6
2
7
3
5
4
8
1
6
2
7
L
R
L
R
swap
swap
3
5
4
8
1
6
2
7
3
5
4
8
1
6
2
7
L
R
L
R
3
5
4
8
1
6
2
7
LR, done
  • 3 is now in place
  • Repeat for left and right partitions recursively
    until partition size1

4
  • Implementation
  • quicksort (1, N, A)
  • function quicksort(L, R, A)
  • m partition(L, R, A)
  • if (mgtL1) quicksort(L, m, A)
  • if (mltR-1) quicksort(m, R, A)
  • function partition(L, R, A)
  • The partitioning algorithm as previously
    described

5
Run-Time Analysis
  • It takes O(n) time to perform the partition
    function on the entire array, whether once as a
    single whole array or multiple times on
    sub-partitions.
  • In the best case
  • each partitioning results in 2 equal or almost
    equal halves
  • the recursive tree structure is a full binary
    tree O(lg n) deep
  • ? best case running time is O(n lg n)
  • In the worst case
  • each partitioning results in the pivot in
    leftmost or right most position
  • the recursive tree structure is therefore a
    straight line O(n) deep
  • ? worst case running time is O(n2)

6
Heapsort
  • Definition
  • A heap is a complete binary tree of height k
    where
  • Each node has is either a leaf node or a parent
    node with 2 children (except possibly the last
    parent which may have only a leaf node for its
    left child)
  • (The last parent is defined as the last non-leaf
    node visited in a breadth-first traversal)
  • All leaf nodes are at heights k and k-1
  • The value of the parent is greater than the
    values of both children
  • e.g. blue is heap, red is not heap

7
Data Structure
  • Number the nodes 1 to N in a breath first manner
    beginning with the root node.
  • Then it follows that for each parent i, the
    children are 2i and 2i1
  • Hence by this convention the nodes 1 to ?n/2? are
    parent nodes while the rest are leaf nodes
  • Heapify
  • To create a heap within a node and its immediate
    descendents
  • Compare node i with its children.
  • If parent node is the largest then we are done
  • Otherwise
  • swap parent node with largest child (say node j)
  • Perform heapify operation on node j recursively
    until leaf node or until no swaps required.

8
Algorithm
  • Step 1
  • Starting from last parent (i.e. node ?n/2? )
    backwards to the first node, perform heapify on
    the node.
  • Step 2
  • Remove root node from heap (this is the largest
    node in the heap, therefore place in at the back
    of the sorted list.
  • Replace with last node in the heap
  • Perform heapify on A0
  • Repeat until heap is empty.

9
Analysis
  • Step 1
  • O(n/2) parents
  • Each parent requires at most O(lg n) heapify
    operations
  • Therefore Step 1 requires O(n lg n) operations.
  • Step 2
  • Each heapify on A0 requires O(lg n) steps
  • Need to heapify A0 n times
  • Therefore Step 2 is also O(n lg n)
  • Running time is therefore O(n lg n)

10
Radix Sort
  • Based on the assumption that the list comprises
    integers at most k binary digits long
  • Assume the digit positions are k-1 to 0
  • Separate list into 2
  • elements with a 0 at position k-1 in top
    sublist
  • elements with a 1 at position k-1 in bottom
    sublist
  • For each sublist, repeat the above step
    recursively, sorting at positions k-2, k-3, k-4
    and so on until position 0.
  • E.g. 3 5 4 1 6 2 7

11
Analysis
  • At every digit position, O(n) operations
    required.
  • k can be assumed to be a constant at most 64 if
    using todays computers
  • So have we found an O(n) sorting algorithm?
  • NO!!!
  • Reason
  • This algorithm is based on certain assumptions
    regarding the data set. In general, no such
    assumptions are made for any of the other sorting
    algorithms.
Write a Comment
User Comments (0)
About PowerShow.com