Lecture 5
This presentation is the property of its rightful owner.
Sponsored Links
1 / 82

Lecture 5 PowerPoint PPT Presentation


  • 57 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 5. Quiz 2. Input: A nonnegative integer n S:=0 for i :=0 to n do S:=S + i * i return S What Doest this Algorithm Compute What is its basic operation How many times is the basic operation executed? What is the efficiency class of this algorithm

Download Presentation

Lecture 5

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 5

Lecture 5


Quiz 2

Quiz 2

Input: A nonnegative integer n

S:=0

for i:=0 to n do

S:=S + i*i

return S

  • What Doest this Algorithm Compute

  • What is its basic operation

  • How many times is the basic operation executed?

  • What is the efficiency class of this algorithm

  • Suggest an improvement or a better algorithm and its efficiency class


Lecture 5

  • Divide and Conquer

  • Merge Sort

  • Quick Sort


Lecture 5

Divide and Conquer

  • Base Case, solve the problem directly if it is small enough

  • Divide the problem into two or more similar and smaller subproblems

  • Recursively solve the subproblems

  • Combine solutions to the subproblems


Divide and conquer sort

Divide and Conquer - Sort

Problem:

  • Input: A[left..right] – unsorted array of integers

  • Output:A[left..right] – sorted in non-decreasing order


Lecture 5

Divide and Conquer - Sort

  • Base case

    • at most one element (left ≥ right), return

  • DivideA into twosubarrays: FirstPart, SecondPart

    • Two Subproblems:

      • sort the FirstPart

      • sort the SecondPart

  • Recursively

    • sort FirstPart

    • sort SecondPart

  • Combinesorted FirstPart and sorted SecondPart


  • Overview

    Overview

    • Divide and Conquer

    • Merge Sort

    • Quick Sort


    Merge sort idea

    Merge

    Merge Sort: Idea

    Divide into

    two halves

    A

    FirstPart

    SecondPart

    Recursively sort

    SecondPart

    FirstPart

    A is sorted!


    Merge sort algorithm

    Merge Sort: Algorithm

    • Merge-Sort(A, left, right)

    • if left ≥ right return

    • else

      • middle ←(left+right)/2

      • Merge-Sort(A, left, middle)

      • Merge-Sort(A, middle+1, right)

      • Merge(A, left, middle, right)

    Recursive Call


    Merge sort merge

    Merge-Sort: Merge

    Sorted

    A:

    merge

    Sorted SecondPart

    SortedFirstPart

    A:

    A[right]

    A[left]

    A[middle]


    Merge sort merge example

    5

    15

    28

    30

    6

    10

    14

    5

    2

    3

    7

    8

    1

    4

    5

    6

    R:

    L:

    3

    5

    15

    28

    6

    10

    14

    22

    Temporary Arrays

    Merge-Sort: Merge Example

    A:


    Merge sort merge example1

    Merge-Sort: Merge Example

    A:

    3

    1

    5

    15

    28

    30

    6

    10

    14

    k=0

    R:

    L:

    3

    2

    15

    3

    7

    28

    30

    8

    6

    1

    10

    4

    5

    14

    22

    6

    i=0

    j=0


    Merge sort merge example2

    Merge-Sort: Merge Example

    A:

    2

    1

    5

    15

    28

    30

    6

    10

    14

    k=1

    R:

    L:

    2

    3

    5

    3

    15

    7

    8

    28

    6

    1

    10

    4

    14

    5

    6

    22

    i=0

    j=1


    Merge sort merge example3

    Merge-Sort: Merge Example

    A:

    3

    1

    2

    15

    28

    30

    6

    10

    14

    k=2

    R:

    L:

    2

    3

    7

    8

    6

    1

    10

    4

    5

    14

    22

    6

    i=1

    j=1


    Merge sort merge example4

    Merge-Sort: Merge Example

    A:

    1

    2

    3

    6

    10

    14

    4

    k=3

    R:

    L:

    2

    3

    7

    8

    1

    6

    10

    4

    14

    5

    6

    22

    j=1

    i=2


    Merge sort merge example5

    Merge-Sort: Merge Example

    A:

    1

    2

    3

    4

    6

    10

    14

    5

    k=4

    R:

    L:

    2

    3

    7

    8

    1

    6

    10

    4

    14

    5

    6

    22

    i=2

    j=2


    Merge sort merge example6

    Merge-Sort: Merge Example

    A:

    6

    1

    2

    3

    4

    5

    6

    10

    14

    k=5

    R:

    L:

    2

    3

    7

    8

    6

    1

    4

    10

    5

    14

    6

    22

    i=2

    j=3


    Merge sort merge example7

    Merge-Sort: Merge Example

    A:

    7

    1

    2

    3

    4

    5

    6

    14

    k=6

    R:

    L:

    2

    3

    7

    8

    1

    6

    10

    4

    14

    5

    22

    6

    i=2

    j=4


    Merge sort merge example8

    Merge-Sort: Merge Example

    A:

    8

    1

    2

    3

    4

    5

    6

    7

    14

    k=7

    R:

    L:

    2

    3

    5

    3

    7

    15

    28

    8

    6

    1

    10

    4

    5

    14

    22

    6

    i=3

    j=4


    Merge sort merge example9

    Merge-Sort: Merge Example

    A:

    1

    2

    3

    4

    5

    6

    7

    8

    k=8

    R:

    L:

    3

    2

    3

    5

    15

    7

    8

    28

    1

    6

    10

    4

    14

    5

    22

    6

    j=4

    i=4


    Lecture 5

    Merge(A, left, middle, right)

    • n1 ← middle – left + 1

    • n2 ← right – middle

    • create array L[n1], R[n2]

    • for i ← 0 to n1-1 do L[i] ← A[left +i]

    • for j ← 0 to n2-1 do R[j] ← A[middle+j]

    • k ← i ← j ← 0

    • while i < n1 & j < n2

    • if L[i] < R[j]

    • A[k++] ← L[i++]

    • else

    • A[k++] ← R[j++]

    • while i < n1

    • A[k++] ← L[i++]

    • while j < n2

    • A[k++] ← R[j++]

    n = n1+n2

    Space: n

    Time : cn for some constant c


    Merge sort a 0 7

    Merge-Sort(A, 0, 7)

    Divide

    A:

    3 7 5 1

    6 2 8 4

    6 2 8 4 3 7 5 1


    Lecture 5

    Merge-Sort(A, 0, 7)

    , divide

    Merge-Sort(A, 0, 3)

    A:

    3 7 5 1

    8 4

    6 2

    6 2 8 4


    Lecture 5

    Merge-Sort(A, 0, 7)

    , divide

    Merge-Sort(A, 0, 1)

    A:

    3 7 5 1

    8 4

    2

    6

    6 2


    Lecture 5

    Merge-Sort(A, 0, 7)

    , base case

    Merge-Sort(A, 0, 0)

    A:

    3 7 5 1

    8 4

    2

    6


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 0, 0), return

    A:

    3 7 5 1

    8 4

    2

    6


    Lecture 5

    Merge-Sort(A, 0, 7)

    , base case

    Merge-Sort(A, 1, 1)

    A:

    3 7 5 1

    8 4

    6

    2


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 1, 1), return

    A:

    3 7 5 1

    8 4

    2

    6


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge(A, 0, 0, 1)

    A:

    3 7 5 1

    8 4

    2 6


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 0, 1), return

    A:

    3 7 5 1

    8 4

    2 6


    Lecture 5

    Merge-Sort(A, 0, 7)

    , divide

    Merge-Sort(A, 2, 3)

    A:

    3 7 5 1

    2 6

    4

    8

    8 4


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 2, 2), base case

    A:

    3 7 5 1

    2 6

    4

    8


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 2, 2),return

    A:

    3 7 5 1

    2 6

    4

    8


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 3, 3), base case

    A:

    2 6

    8

    4


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 3, 3), return

    A:

    3 7 5 1

    2 6

    4

    8


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge(A, 2, 2, 3)

    A:

    3 7 5 1

    2 6

    4 8


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 2, 3), return

    A:

    3 7 5 1

    4 8

    2 6


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge(A, 0, 1, 3)

    A:

    3 7 5 1

    2 4 6 8


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 0, 3), return

    A:

    2 4 6 8

    3 7 5 1


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 4, 7)

    A:

    2 4 6 8

    3 7 5 1


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge (A, 4, 5, 7)

    A:

    2 4 6 8

    1 3 5 7


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 4, 7), return

    A:

    2 4 6 8

    1 3 5 7


    Lecture 5

    Merge-Sort(A, 0, 7)

    Merge-Sort(A, 0, 7), done!

    Merge(A, 0, 3, 7)

    A:

    1 2 3 4 5 6 7 8


    Lecture 5

    Merge-Sort Analysis

    cn

    n

    2 × cn/2 = cn

    n/2

    n/2

    log n levels

    4 × cn/4 = cn

    n/4

    n/4

    n/4

    n/4

    n/2 × 2c = cn

    2

    2

    2

    Total: cn log n

    • Total running time:(nlogn)

    • Total Space: (n)


    Merge sort summary

    Merge-Sort Summary

    Approach: divide and conquer

    Time

    • Most of the work is in the merging

    • Total time: (n log n)

      Space:

    • (n), more space than other sorts.


    Overview1

    Overview

    • Divide and Conquer

    • Merge Sort

    • Quick Sort


    Quick sort

    Quick Sort

    • Divide:

      • Pick any element p as the pivot, e.g, the first element

      • Partition the remaining elements into

        FirstPart,which contains all elements< p

        SecondPart, which contains all elements ≥ p

  • Recursively sort the FirstPart and SecondPart

  • Combine: no work is necessary since sorting

    is done in place


  • Quick sort1

    Partition

    Recursive call

    p

    p

    p

    p ≤ x

    p ≤ x

    Sorted

    FirstPart

    Sorted

    SecondPart

    x < p

    Quick Sort

    A:

    pivot

    FirstPart

    SecondPart

    x < p

    Sorted


    Quick sort2

    Quick Sort

    Quick-Sort(A, left, right)

    ifleft ≥ right return

    else

    middle ← Partition(A, left, right)

    Quick-Sort(A, left, middle–1 )

    Quick-Sort(A, middle+1, right)

    end if


    Partition

    p

    x < p

    p

    p ≤ x

    x < p

    p ≤ x

    p

    Partition

    A:

    A:

    A:

    p


    Partition example

    Partition Example

    A:

    4

    8

    6

    3

    5

    1

    7

    2


    Partition example1

    Partition Example

    i=0

    A:

    4

    8

    6

    3

    5

    1

    7

    2

    j=1


    Partition example2

    Partition Example

    i=0

    A:

    4

    8

    6

    3

    5

    1

    7

    2

    8

    j=1


    Partition example3

    Partition Example

    i=0

    A:

    4

    8

    6

    6

    3

    5

    1

    7

    2

    j=2


    Partition example4

    i=1

    3

    8

    Partition Example

    i=0

    A:

    4

    6

    3

    5

    1

    7

    2

    8

    3

    j=3


    Partition example5

    i=1

    Partition Example

    A:

    4

    3

    6

    8

    5

    1

    7

    2

    5

    j=4


    Partition example6

    i=1

    Partition Example

    A:

    4

    3

    6

    8

    5

    1

    1

    7

    2

    j=5


    Partition example7

    i=2

    1

    6

    Partition Example

    A:

    4

    3

    6

    8

    5

    1

    7

    2

    j=5


    Partition example8

    i=2

    Partition Example

    A:

    7

    4

    3

    1

    8

    5

    6

    7

    2

    j=6


    Partition example9

    i=2

    i=3

    Partition Example

    A:

    8

    2

    2

    4

    3

    1

    8

    5

    6

    7

    2

    j=7


    Partition example10

    i=3

    Partition Example

    A:

    4

    3

    1

    2

    5

    6

    7

    8

    j=8


    Partition example11

    i=3

    Partition Example

    A:

    4

    4

    3

    1

    2

    5

    6

    7

    8

    2


    Partition example12

    x < 4

    4 ≤ x

    Partition Example

    pivot in

    correct position

    A:

    2

    4

    3

    1

    5

    6

    7

    8


    Lecture 5

    Partition(A, left, right)

    • x ← A[left]

    • i ← left

    • for j ← left+1 to right

    • if A[j] < x then

    • i ← i + 1

    • swap(A[i], A[j])

    • end if

    • end for j

    • swap(A[i], A[left])

    • return i

    n = right – left +1

    Time: cn for some constant c

    Space: constant


    Quick sort a 0 7

    2 3 1

    4

    5 6 7 8

    Quick-Sort(A, 0, 7)

    Partition

    A:

    4 8 6 3 5 1 7 2


    Lecture 5

    1

    3

    2

    Quick-Sort(A, 0, 7)

    , partition

    Quick-Sort(A, 0, 2)

    A:

    5 6 7 8

    4

    2 3 1


    Lecture 5

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 0, 0)

    , base case

    , return

    5 6 7 8

    4

    3

    1

    2

    1


    Lecture 5

    3

    3

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 1, 1)

    , base case

    5 6 7 8

    4

    1

    2


    Lecture 5

    1

    1

    3

    3

    2

    2

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 0, 2), return

    Quick-Sort(A, 2, 2),return

    5 6 7 8

    4


    Lecture 5

    5 6 7 8

    6 7 8

    5

    1

    3

    2

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 4, 7)

    , partition

    Quick-Sort(A, 2, 2),return

    4


    Lecture 5

    1

    3

    2

    6

    7 8

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 5, 7)

    , partition

    4

    5

    6

    6 7 8


    Lecture 5

    1

    3

    2

    Quick-Sort(A, 0, 7)

    Quick-Sort(A, 6, 7)

    , partition

    4

    5

    6

    7

    7 8

    8


    Lecture 5

    1

    3

    2

    Quick-Sort(A, 0, 7)

    , return

    , base case

    Quick-Sort(A, 7, 7)

    4

    5

    6

    7

    8

    8


    Lecture 5

    1

    3

    2

    Quick-Sort(A, 0, 7)

    , return

    Quick-Sort(A, 6, 7)

    4

    5

    6

    7

    8


    Lecture 5

    1

    3

    2

    6

    7

    8

    Quick-Sort(A, 0, 7)

    , return

    Quick-Sort(A, 5, 7)

    4

    5


    Lecture 5

    5

    1

    3

    2

    6

    7

    8

    Quick-Sort(A, 0, 7)

    , return

    Quick-Sort(A, 4, 7)

    4


    Lecture 5

    5

    1

    3

    2

    6

    7

    8

    Quick-Sort(A, 0, 7)

    , done!

    Quick-Sort(A, 0, 7)

    4


    Lecture 5

    cn

    n

    n/2

    n/2

    2 × cn/2 = cn

    log n levels

    n/4

    n/4

    n/4

    4 × c/4 = cn

    n/4

    n/3 × 3c = cn

    3

    3

    3

    Quick-Sort: Best Case

    • Even Partition

    Total time: (nlogn)


    Quick sort worst case

    Quick-Sort: Worst Case

    • Unbalanced Partition

    cn

    n

    c(n-1)

    n-1

    c(n-2)

    n-2

    3c

    3

    Happens only if

    • input is sortd

    • input is reversely sorted

    2c

    2

    Total time: (n2)


    Quick sort an average case

    Quick-Sort: an Average Case

    Quick-Sort: an Average Case

    • Suppose the split is 1/10 : 9/10

    cn

    n

    cn

    0.1n

    0.9n

    log10n

    0.81n

    0.09n

    0.01n

    0.09n

    cn

    log10/9n

    2

    ≤cn

    ≤cn

    2

    Total time: (nlogn)


    Quick sort summary

    Quick-Sort Summary

    • Time

      • Most of the work done in partitioning.

      • Average case takes (n log(n)) time.

      • Worst case takes (n2) time

        Space

      • Sorts in-place, i.e., does not require additional space


    Summary

    Summary

    • Divide and Conquer

    • Merge-Sort

      • Most of the work done in Merging

      • (n log(n)) time

      • (n) space

    • Quick-Sort

      • Most of the work done in partitioning

      • Average case takes (n log(n)) time

      • Worst case takes (n2) time

      • (1) space


  • Login