Running times continued - some running times are more difficult to - - PowerPoint PPT Presentation

running times continued
SMART_READER_LITE
LIVE PREVIEW

Running times continued - some running times are more difficult to - - PowerPoint PPT Presentation

Running times continued - some running times are more difficult to analyze Merging two sorted lists Merging two sorted lists Input: Two arrays A = {a 1 , a 2 , , a m }, B = {b 1 , b 2 , , b n }, in increasing order in increasing order


slide-1
SLIDE 1

Running times continued

  • some running times are more difficult to analyze

Merging two sorted lists Merging two sorted lists Input: Two arrays A = {a1, a2, …, am}, B = {b1, b2, …, bn}, in increasing order Output: in increasing order Array C containing A ∪ B, in increasing order

slide-2
SLIDE 2

Running times continued

Merging two sorted lists Input: Two arrays A = {a1, a2, …, am}, B = {b1, b2, …, bn}, i i i d Output: in increasing order Array C containing A ∪ B, in increasing order p y g g Running time ? MERGE(A,B)

  • 1. i=1; j=1; k=1;

g

  • 2. am+1=∞; bn+1=∞;
  • 3. while (k ≤ m+n) do

4. if (ai < bj) then . (ai bj) t e 5. ck=ai; i++; 6. else 7 c =b ; j++; 7. ck=bj; j++; 8. k++;

  • 9. RETURN C={c1, c2, …, cm+n}
slide-3
SLIDE 3

Running times continued

Sorting Input: An array X = {x1, x2, …, xn} Output: X sorted in increasing order

slide-4
SLIDE 4

Running times continued

Sorting Input: An array X = {x1, x2, …, xn} Output: X sorted in increasing order MERGESORT(X,n) MergeSort – a divide-and-conquer algorithm ( , )

  • 1. if (n == 1) RETURN X
  • 2. middle = n/2 (round down)

3 A = {x x x }

  • 3. A = {x1, x2, …, xmiddle}
  • 4. B = {xmiddle+1, xmiddle+2, …, xn}
  • 5. As = MERGESORT(A,middle)
  • 6. Bs = MERGESORT(B,n-middle)
  • 7. RETURN MERGE(As,Bs)
slide-5
SLIDE 5

Running times continued

Sorting Input: An array X = {x1, x2, …, xn} Output: X sorted in increasing order MergeSort Running time ? MERGESORT(X,n) Running time ? ( , )

  • 1. if (n == 1) RETURN X
  • 2. middle = n/2 (round down)

3 A = {x x x }

  • 3. A = {x1, x2, …, xmiddle}
  • 4. B = {xmiddle+1, xmiddle+2, …, xn}
  • 5. As = MERGESORT(A,middle)
  • 6. Bs = MERGESORT(B,n-middle)
  • 7. RETURN MERGE(As,Bs)
slide-6
SLIDE 6

A recurrence

Running time of MergeSort: T(n) How to bound T(n) ?

  • > “unrolling the recurrence”
slide-7
SLIDE 7

A recurrence

Running time of MergeSort: T(n) How to bound T(n) ?

  • > “substitution / induction”
slide-8
SLIDE 8

More on sorting

Other O(n log n) sorts ? Can do better than O(n log n) ?

slide-9
SLIDE 9

More on sorting

HeapSort

  • underlying datastructure: heap

A heap is a complete binary tree, with nodes storing Def: keys, and the property that for every parent and child: key(parent) ≤ key(child).

slide-10
SLIDE 10

More on sorting

HeapSort

  • underlying datastructure: heap

Use: priority queue – a datastructure that supports:

  • extract-min
  • add key
  • change key value
slide-11
SLIDE 11

More on sorting

Heap

  • stored in an array – how to compute:

Parent Left child

  • how to add a key ?

Right child y

slide-12
SLIDE 12

More on sorting

Heap

  • stored in an array – how to compute:

Parent(i) = i/2 LeftChild(i) = 2i

  • how to add a key ?

RightChild(i) = 2i+1 y HEAPIFY-UP(H,i) 1 while (i > 1) and (H[i] < H[Parent(i)]) do

  • 1. while (i > 1) and (H[i] < H[Parent(i)]) do

2. swap entries H[i] and H[Parent(i)] 3. i = Parent(i) ADD(H,key) 1 H size++

  • 1. H.size++
  • 2. H[H.size] = key
  • 3. HEAPIFY-UP(H,H.size)
slide-13
SLIDE 13

More on sorting

Heap

  • stored in an array – how to compute:

Parent(i) = i/2 LeftChild(i) = 2i

  • what if we change the value of a key (at position i) ?

RightChild(i) = 2i+1 g y ( p )

  • if key decreased, then:
  • otherwise ?
  • otherwise ?
slide-14
SLIDE 14

More on sorting

Heap

  • stored in an array – how to compute:

Parent(i) = i/2 LeftChild(i) = 2i

  • what if we change the value of a key (at position i) ?

RightChild(i) = 2i+1 g y ( p )

HEAPIFY-DOWN(H,i)

  • 1. n = H.size
  • 2. while (LeftChild(i)<=n and H[i] > H[LeftChild(i)])
  • r (RightChild(i)<=n and H[i] > H[RightChild(i)]) do

3. if (H[LeftChild(i)] < H[RightChild(i)]) then 4. j = LeftChild(i) 5. else 6. j = RightChild(i) 7 swap entries H[i] and H[j] 7. swap entries H[i] and H[j] 8. i = j

slide-15
SLIDE 15

More on sorting

Heap

  • running times:

Use: priority queue – a datastructure that supports:

  • extract-min
  • add key
  • change key value
slide-16
SLIDE 16

More on sorting

HeapSort HEAPSORT(A) 1 H BUILD HEAP(A)

  • 1. H = BUILD_HEAP(A)
  • 2. n = A.length
  • 3. for i=1 to n do

4. A[i] = EXTRACT_MIN(H) BUILD HEAP(A) EXTRACT MIN(H) _ ( )

  • 1. initially H = ∅
  • 2. n = A.length

3 for i=1 to n do _ ( )

  • 1. min = H[1]
  • 2. H[1] = H[H.size]

3 H size--

  • 3. for i=1 to n do

4. ADD(H,A[i])

  • 3. H.size--
  • 4. HEAPIFY_DOWN(H,1)
  • 5. RETURN min

Note (more efficient BUILD_HEAP): A different implementation of BUILD_HEAP runs in time O(n).

slide-17
SLIDE 17

More on sorting

HeapSort HEAPSORT(A) 1 H BUILD HEAP(A)

  • 1. H = BUILD_HEAP(A)
  • 2. n = A.length
  • 3. for i=0 to n-1 do

4. A[i] = EXTRACT_MIN(H) BUILD HEAP(A) EXTRACT MIN(H) _ ( )

  • 1. initially H = ∅
  • 2. n = A.length

3 for i=0 to n-1 do _ ( )

  • 1. min = H[0]
  • 2. H.length--

3 H[0] = H[H length]

  • 3. for i=0 to n-1 do

4. ADD(H,A[i])

  • 3. H[0] = H[H.length]
  • 4. HEAPIFY_DOWN(H,0)
  • 5. RETURN min

Running time :

slide-18
SLIDE 18

Related datastructures

slide-19
SLIDE 19

A lower-bound on sorting: (n log n)

Every comparison-based sort needs at least (n log n) comparisons, thus it’s running time is (n log n).

slide-20
SLIDE 20

Sorting faster than O(n log n) ?

We know: Every comparison-based sort needs at least (n log n) i comparisons. Can we possibly sort faster than O(n log n) ? p y ( g )

slide-21
SLIDE 21

Sorting faster than O(n log n) ?

RadixSort – a non-comparison based sort. Idea: First sort the input by the last digit Idea: First sort the input by the last digit.

slide-22
SLIDE 22

Sorting faster than O(n log n) ?

RadixSort – a non-comparison based sort.

RADIXSORT(A)

  • 1. d = length of the longest element in A
  • 2. for j=1 to d do

3. COUNTSORT(A,j) // a stable sort to sort A // b th j th l t di it // by the j-th last digit COUNTSORT (A,j) 1 9

  • 1. let B[0..9] be an array of (empty) linked-lists
  • 2. n = A.length
  • 3. for i=0 to n-1 do

4 let x be the j th last digit of A[i] 4. let x be the j-th last digit of A[i] 5. add A[i] at the end of the linked-list B[x]

  • 6. copy B[1] followed by B[2], then B[3], etc. to A

Running time ?

slide-23
SLIDE 23

Sorting faster than O(n log n) ?

RadixSort – a non-comparison based sort. Idea: First sort the input by the last digit Idea: First sort the input by the last digit.

slide-24
SLIDE 24

Sorting faster than O(n log n) ?

RadixSort – a non-comparison based sort.

RADIXSORT(A)

  • 1. d = length of the longest element in A
  • 2. for j=1 to d do

3. COUNTSORT(A,j) // a stable sort to sort A // b th j th l t di it // by the j-th last digit COUNTSORT (A,j) 1 9

  • 1. let B[0..9] be an array of (empty) linked-lists
  • 2. n = A.length
  • 3. for i=1 to n do

4 let x be the j th last digit of A[i] 4. let x be the j-th last digit of A[i] 5. add A[i] at the end of the linked-list B[x]

  • 6. copy B[1] followed by B[2], then B[3], etc. to A

Running time ?