1 / 22

Running times continued

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

welke
Download Presentation

Running times continued

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Running times continued - some running times are more difficult to analyze Merging two sorted lists Input: Output: Two arrays A = {a1, a2, …, am}, B = {b1, b2, …, bn}, in increasing order Array C containing A [ B, in increasing order

  2. Running times continued Merging two sorted lists Input: Output: Two arrays A = {a1, a2, …, am}, B = {b1, b2, …, bn}, in increasing order Array C containing A [ B, in increasing order • MERGE(A,B) • i=1; j=1; k=1; • am+1=1; bn+1=1; • while (k · m+n) do • if (ai < bj) then • ck=ai; i++; • else • ck=bj; j++; • k++; • RETURN C={c1, c2, …, cm+n} Running time ?

  3. Running times continued Sorting Input: Output: An array X = {x1, x2, …, xn} X sorted in increasing order

  4. Running times continued Sorting Input: Output: An array X = {x1, x2, …, xn} X sorted in increasing order MergeSort – a divide-and-conquer algorithm • MERGESORT(X,n) • if (n == 1) RETURN X • middle = n/2 (round down) • A = {x1, x2, …, xmiddle} • B = {xmiddle+1, xmiddle+2, …, xn} • As = MERGESORT(A,middle) • Bs = MERGESORT(B,n-middle) • RETURN MERGE(As,Bs)

  5. Running times continued Sorting Input: Output: An array X = {x1, x2, …, xn} X sorted in increasing order MergeSort • MERGESORT(X,n) • if (n == 1) RETURN X • middle = n/2 (round down) • A = {x1, x2, …, xmiddle} • B = {xmiddle+1, xmiddle+2, …, xn} • As = MERGESORT(A,middle) • Bs = MERGESORT(B,n-middle) • RETURN MERGE(As,Bs) Running time ?

  6. A recurrence Running time of MergeSort: T(n) How to bound T(n) ? -> “unrolling the recurrence”

  7. A recurrence Running time of MergeSort: T(n) How to bound T(n) ? -> “substitution / induction”

  8. More on sorting Other O(n log n) sorts ? Can do better than O(n log n) ?

  9. More on sorting HeapSort - underlying datastructure: heap Def: A heap is a complete binary tree, with nodes storing keys, and the property that for every parent and child: key(parent) · key(child).

  10. More on sorting HeapSort - underlying datastructure: heap • Use: priority queue – a datastructure that supports: • extract-min • add key • change key value

  11. More on sorting • Heap • stored in an array – how to compute: • how to add a key ? Parent Left child Right child

  12. More on sorting • Heap • stored in an array – how to compute: • how to add a key ? Parent(i) = (i-1)/2 LeftChild(i) = 2i+1 RightChild(i) = 2i+2 • HEAPIFY-UP(H,i) • while (i > 0) and (H[i] < H[Parent(i)]) do • swap entries H[i] and H[Parent(i)] • i = Parent(i) • ADD(H,key) • H[H.length] = key • H.length++ • HEAPIFY-UP(H,H.length)

  13. More on sorting • Heap • stored in an array – how to compute: • what if we change the value of a key (at position i) ? • - if key decreased, then: • - otherwise ? Parent(i) = (i-1)/2 LeftChild(i) = 2i+1 RightChild(i) = 2i+2

  14. More on sorting • Heap • stored in an array – how to compute: • what if we change the value of a key (at position i) ? Parent(i) = (i-1)/2 LeftChild(i) = 2i+1 RightChild(i) = 2i+2 • HEAPIFY-DOWN(H,i) • n = H.length • while (LeftChild(i)<n and H[i] > H[LeftChild(i)]) • or (RightChild(i)<n and H[i] > H[RightChild(i)]) do • if (H[LeftChild(i)] < H[RightChild(i)]) then • j = LeftChild(i) • else • j = RightChild(i) • swap entries H[i] and H[j] • i = j

  15. More on sorting Heap - running times: • Use: priority queue – a datastructure that supports: • extract-min • add key • change key value

  16. More on sorting HeapSort • HEAPSORT(A) • H = BUILD_HEAP(A) • n = A.length • for i=0 to n-1 do • A[i] = EXTRACT_MIN(H) • BUILD_HEAP(A) • initially H = ; • n = A.length • for i=0 to n-1 do • ADD(H,A[i]) • EXTRACT_MIN(H) • min = H[0] • H.length-- • H[0] = H[H.length] • HEAPIFY_DOWN(H,0) • RETURN min Note (more efficient BUILD_HEAP): A different implementation of BUILD_HEAP runs in time O(n).

  17. More on sorting HeapSort • HEAPSORT(A) • H = BUILD_HEAP(A) • n = A.length • for i=0 to n-1 do • A[i] = EXTRACT_MIN(H) • BUILD_HEAP(A) • initially H = ; • n = A.length • for i=0 to n-1 do • ADD(H,A[i]) • EXTRACT_MIN(H) • min = H[0] • H.length-- • H[0] = H[H.length] • HEAPIFY_DOWN(H,0) • RETURN min Running time :

  18. Related datastructures

  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).

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

  21. Sorting faster than O(n log n) ? RadixSort – a non-comparison based sort. Idea: First sort the input by the last digit.

  22. Sorting faster than O(n log n) ? RadixSort – a non-comparison based sort. • RADIXSORT(A) • d = length of the longest element in A • for j=1 to d do • COUNTSORT(A,j) // a stable sort to sort A • // by the j-th last digit • COUNTSORT (A,j) • let B[0..9] be an array of (empty) linked-lists • n = A.length • for i=0 to n-1 do • let x be the j-th last digit of A[i] • add A[i] at the end of the linked-list B[x] • copy B[1] followed by B[2], then B[3], etc. to A Running time ?

More Related