Tutorial 6: Quick Sort - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Tutorial 6: Quick Sort

Description:

After adding the node, since it destroy the heap property, we will Percolate up ... Re-heap solution. Remove the rightmost leaf at the deepest level and use it ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 33
Provided by: sdz
Category:

less

Transcript and Presenter's Notes

Title: Tutorial 6: Quick Sort


1
Tutorial 6 Quick Sort heap sort
  • Quick sort
  • Heap sort
  • Empirical Analysis

2
Introduction
  • Quick sort, like merge sort, is based on the
    divide-and-conquer paradigm.
  • Intuitively, it operates as follows.
  • Divide
  • CHOOSE A PIVOT ELEMENT in the list.
  • PARTITION the list into two parts
  • one contains elements SMALLER THAN
    this pivot
  • another contains elements LARGER THAN
    OR EQUAL TO this pivot.
  • Note the pivot is already in its FINAL POSITION
    in the sorted array after this step.

3
Introduction(cont.)
  • Conquer
  • Recursively sort the two partitions using quick
    sort.
  • Combine
  • Since the two partitions are sorted in place, no
    work is
  • needed to combine them. The entire array is now
    sorted.

4
Partition
  • How to choose a pivot and perform partition?
  • Lets say we want to sort an array Apr.
    Assume that
  • we select the last element in the array (x
    Ar) as a
  • pivot. Then we start to partition the array into
    two
  • Segments.

5
Partition
  • Maintain two variables i and j with initial
  • values of p and r-1 respectively.

6
Partition
  • When i lt j,
  • Move i right, skipping over elements smaller than
    the pivot.
  • Move j left, skipping over elements greater than
    the pivot.
  • When both i and j have stopped, swap Ai and
    Aj and this
  • pushes the large element to the right and pull
    the small element to
  • the left.
  • Repeat this process until i and j cross.
  • When i and j have crossed, swap Ai and pivot.

7
Example 1
  • An dynamic example showing how quick sort works.

8
Example 2
9
Example(cont.)
10
Pseudocode of Quick Sort
  • void quicksort(A, p, r)
  • int pivot Ar
  • // begin partitioning
  • int i p
  • int j r-1
  • for()
  • while(Ai lt pivot) i
  • while(Aj gt pivot) j--
  • if(i lt j)
  • swap(Ai, Aj)
  • else
  • break

swap(Ai, Ar) // recursive sort each
partition quicksort(A, p, i-1) // Sort left
partition quicksort(A, i1, r) // Sort right
partition
11
Running time analysis
  • To analyze the running time, we focus on the
    number of
  • comparisons needed.
  • Worst case Let T(n) denote the worst-case
    running time of quicksort.
  • The total running time involves the following.
  • Pivot selection O(1)
    time
  • Partitioning O(n)
    time
  • Running time of two
    recursive steps
  • Therefore
  • T(n) T(i) T(n - i
    - 1) cn
  • where i is the number of elements in the first
    partition and c is a constant.

12
Running time analysis
  • The worst case occurs while
  • 1.The pivot is the smallest element, all the
    time.
  • 2.Partition is always UNBALANCED.

13
Running time analysis
14
Running time analysis
  • Best case
  • The best case occurs while
  • The pivot is always in the middle (median
    of the array).
  • Partition is perfectly BALANCED.
  • Note Assume that n is a power of 2.

15
(No Transcript)
16
Heapsort
  • Definition of Heap
  • balanced and complete (except the bottom level)
    binary tree
  • every node always lt (or gt) its children
  • Heapsort is always O(n log n)
  • Quicksort is usually O(n log n) but in the worst
    case slows to O(n2)
  • Quicksort is generally faster, but Heapsort is
    better in time-critical application

17
Heap properties
  • When stored in an array A
  • If Ai has parent, then the parent is A?i/2?.
  • If Ai has left child, then it is A2i.
  • If Ai has right child, then it is A2i1.
  • Depth Ai A?log i?
  • Root node is the minimum / maximum

18
A (max) heap stored in an array
19
Construct a heap (heapify)
  • A tree with a single node is automatically a heap
  • We construct a heap by inserting nodes one at a
    time
  • to the right of the rightmost node in the deepest
    level
  • If the deepest level is full, start a new level

20
Construct a heap (heapify) cont.
  • After adding the node, since it destroy the heap
    property, we will Percolate up it up by comparing
    with its parent
  • Example (given 4 numbers, 8,10,5,12)

8
1
2
3
4
21
Heap sort
  • Sorting can be performed by
  • Keep deleting the root node of the max-heap
  • Descending order
  • Keep deleting the root node of the min-heap
  • Ascending order
  • Problem
  • Delete operation may destroy heap property
  • So after deleting the root, we have to re-heap
    the tree

22
Delete
  • Re-heap solution
  • Remove the rightmost leaf at the deepest level
    and use it for the new root
  • Percolate down the root node by comparing with
    its children

23
Delete example
24
Delete example
25
Delete example
26
Delete example
27
Delete example
28
Delete example
29
Time analysis
  • Heapify the array O(n log n) time
  • Keep deleting the root and re-heap
  • Takes O(n log n)
  • So the overall complexity is O(n log n) time

30
Some Empirical Analysis
31
Some Empirical Analysis
32
The best sorting algorithm
  • The empirical data here is the average of a
  • hundred runs against random data sets.
  • Keep in mind that "best" depends on your
  • situation - the quick sort may look like the
    fastest
  • sort, but using it to sort a list of 20 items is
    kind
  • of like going after a fly with a sledgehammer.
Write a Comment
User Comments (0)
About PowerShow.com