SORTING SEARCHING DIVIDE & CONQUER · PDF file 2020. 3. 30. · Insertion sort...

Click here to load reader

  • date post

    02-Feb-2021
  • Category

    Documents

  • view

    0
  • download

    0

Embed Size (px)

Transcript of SORTING SEARCHING DIVIDE & CONQUER · PDF file 2020. 3. 30. · Insertion sort...

  • SORTING SEARCHING

    DIVIDE & CONQUER

    Nikhil Kumar

  • Sorting

    Input: sequence a1, a2, …, an of numbers.

    Example:

    Input: 8 2 4 9 3 6

    Output: 2 3 4 6 8 9

    Output: permutation a'1, a'2, …, a'n such

    that a'1  a'2  …  a'n .

  • Insertion sort

    INSERTION-SORT (A, n) ⊳ A[1 . . n] for j ← 2 to n

    do key ← A[ j]

    i ← j – 1

    while i > 0 and A[i] > key

    do A[i+1] ← A[i]

    i ← i – 1

    A[i+1] = key

    “Algorithm”

    i j

    key sorted

    A: 1 n

  • Example of insertion sort

    8 2 4 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

    2 4 8 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

    2 4 8 9 3 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

    2 4 8 9 3 6

    2 3 4 8 9 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

    2 4 8 9 3 6

    2 3 4 8 9 6

  • Example of insertion sort

    8 2 4 9 3 6

    2 8 4 9 3 6

    2 4 8 9 3 6

    2 4 8 9 3 6

    2 3 4 8 9 6

    2 3 4 6 8 9 done

  • Insertion sort analysis

    Worst case: Input reverse sorted.

      

     n

    j

    njnT 2

    2)()(

    Average case: All permutations equally likely.

      

     n

    j

    njnT 2

    2)2/()(

    Is insertion sort a fast sorting algorithm?

    • Moderately so, for small n.

    • Not at all, for large n.

    [arithmetic series]

  • Divide and Conquer

  • Algorithm

    D&C (P) {

    If( small(P))

    {then return S(P)

    }

    else{

    divide P into smaller subproblems P1 ,P2. .Pk Apply D&C to each sub problem

    Return (combine (D&C(P1)+ D&C(P2) + ..+D&C(Pk))

    }

    }

  • Recurrence relation of D &C

    T(n)= ϴ(1) if n

  • L2.7

    Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.Example: Find 9

    3 5 7 8 9 12 15

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

    • Example: Find 9

    3 5 7 8 9 12 15

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

    • Example: Find 9

    3 5 7 8 9 12 15

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

    • Example: Find 9

    3 5 7 8 9 12 15

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

    • Example: Find 9

    3 5 7 8 9 12 15

  • Binary search

    • Find an element in a sorted array:

    1. Divide: Check middle element.

    2. Conquer: Recursively search 1 subarray.

    3. Combine: Trivial.

    • Example: Find 9

    3 5 7 8 9 12 15

  • September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14

    Recurrence for binary search

    T(n) = 1 T(n/2) +(1)

    # subproblems

    subproblem size

    work dividing and combining

  • September 14, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.15

    Recurrence for binary search

    T(n) = 1 T(n/2) +(1)

    # subproblems

    subproblem size

    work dividing and combining

    nlogba = nlog21 = n0 = 1  CASE 2 (k =0)

     T(n) = (lg n) .

  • MERGE SORT

    L1.29

  • MERGE-SORT A[1 . . n]

    1. If n = 1, done.

    2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] .

    3. “Merge” the 2 sorted lists.

    Key subroutine: MERGE

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

    20

    13

    12

    11

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

    20

    13

    12

    11

    11

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

    20

    13

    12

    11

    11

    20

    13

    12

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

    20

    13

    12

    11

    11

    20

    13

    12

    12

  • Merging two sorted arrays

    20

    13

    7

    2

    12

    11

    9

    1

    1

    20

    13

    7

    2

    12

    11

    9

    2

    20

    13

    7

    12

    11

    9

    7

    20

    13

    12

    11

    9

    9

    20

    13

    12

    11

    11

    20

    13

    12

    12

    Time = (n) to merge a total

    of n elements (linear time).

  • Analyzing me