Algorithm Insertion Sort: Specification for Subroutine In

Algorithm Insertion Sort: Specification for Subroutine In

Problem: Sorting Application of Sorting 4 arranging elements of set into order • For searching on unsorted data by comparing keys, • Algorithm design technique: optimal solutions require θ(n) comparisons. 4 Divide and Conquer • For searching on sorted data by comparing keys, • Solution: optimal solutions require θ(log n) comparisons. 4 Insertion Sort • Sorting data for users 4 Quicksort • More… 4 Mergesort 4 Heapsort 4 Shellsort 4 Radix Sorting TECH • Optimality: Computer Science 4 Lower bounds for Sorting by Comparison of Keys Insertion Sort Insertion Sort: Algorithm • Strategy: • Input 4Insertion of an element in proper order: 4E, an array of elements, and n >=0, the number of 4Begin with a sequence E of n elements in arbitrary elements. The range of indexes is 0, …, n-1 order • Output 4Initially assume the sorted segment contains first 4E, with elements in nondecreasing order of their keys element • void insertionSort(Element[] E, int n) 4Let x be the next element to be inserted in sorted segment, pull x “out of the way”, leaving a vacancy 4int xindex; 4repeatedly compare x to the element just to the left of 4for (xindex = 1; xindex < n; xindex++) the vacancy, and as long as x is smaller, move that f Element current = E[xindex]; element into the vacancy, f key x = current.key int xLoc = shiftVacRec(E, xindex, x); 4else put x in the vacancy, f f E[xLoc] = current; 4repeat the next element that has not yet examined. 4return; Insertion Sort: Specification for subroutine Insertion Sort: Algorithm shiftVacRec • Specification • int shiftVacRec(Element[] E, int vacant, Key x) 4int shiftVacRec(Element[] E, int vacant, Key x) 4int xLoc; 4Precondition 4if (vacant == 0) f Vacant is nonnegative f xLoc = vacant; 4Postconditions 4else if (E[vacant-1].key <= x) f 1. Elements in E at indexes less than xLoc are in their original f xLoc = vacant; positions and have keys less than or equal to x 4else 2. Elements in E at positions xLoc+1, …, vacant are greater than x f E[vacant] = E[vacant-1]; and were shifted up by one position from their positions when f shiftVacRec was invoked. f xLoc = shiftVacRec(E, vacant-1, x); 4return xLoc 1 Insertion Sort: Analysis Insertion Sort: Optimality • Worst-Case Complexity • Theorem 4.1 4W(n) = ∑{i=1 to n-1}[i] = n(n-1)/2 ∈θ(n2) 4Any algorithm that sorts by comparison of keys and • Average Behavior removes at most one inversion after each comparison must do at least n(n-1)/2 comparisons in the worst case 4average number of comparisons in shiftVacRec and at least n(n-1)/4 comparisons on the average (for n 41/(i+1) ∑{i=1 to j} (j) + i/(i+1) = i/2+1-1/(i+1) elements) 4A(n) = ∑{i=1 to n-1} [1/2+1-1/(i+1)] ≈ (n^2)/4 • Proof… • Insertion Sort is optimal for algorithms that works “locally” by interchanging only adjacent elements. • But, it is not the best sorting algorithm. Algorithm Design Technique: Divide and Conquer Using Divide and Conquer: Mergesort 4It is often easier to solve several small instances of a • Mergesort Strategy problem than one large one. f divide the problem into smaller instances of the same problem f solve (conquer) the smaller instances recursively f combine the solutions to obtain the solution for original input • Solve(I) 4n = size(I) 4if (n <= smallsize) f solution = directlySolve(I); 4else f divide I into I1, …, Ik. f for each i in {1, …, k} • Si = solve(Ii); f solution = combine(S1, …, Sk); 4return solution; Algorithm: Mergesort Merging Sorted Sequences 4Input: Array E and indexs first, and Last, such that the • Problem: elements E[i] are defined for first <= i <= last. 4Given two sequences A and B sorted in nondecreasing 4Output: E[first], …, E[last] is sorted rearrangement of order, merge them to create one sorted sequence C the same elements • void mergeSort(Element[] E, int first, int last) • Strategy: 4if (first < last) 4determine the first item in C: It is the minimum f int mid = (first+last)/2; between the first items of A and B. Suppose it is the first f mergeSort(E, first, mid); items of A. Then, rest of C consisting of merging rest of f mergeSort(E, mid+1, last); A with B. f merge(E, first, mid, last); 4return; • W(n) = W(n/2)+W(n/2) + Wmerge(n) ∈θ(n log n) 4Wmerge(n) = n-1 4W(1) = 0 2 Algorithm: Merge Heap and Heapsort • Merge(A, B, C) • A Heap data structure is a binary tree with special properties: 4if (A is empty) 4 Heap Structure 4 Partial order tree property f rest of C = rest of B 4else if (B is empty) • Definition: Heap Structure 4 A binary tree T is a heap structure if and only if it satisfies the rest of C = rest of A f following conditions: (h = height of the tree) 4else if (first of A <= first of B) f 1. T is complete at least through depth h-1 f first of C = first of A f 2. All leaves are at depth h or h –1 f merge (rest of A, B, rest of C) f 3. All paths to leaf of depth h are to the left of all parts to a leaf of depth h- 4else 1 4 Such a tree is also called a left-complete binary tree. f first of C = first of B • Definition: Partial order tree property f merge (A, rest of B, rest of C) 4return 4 A tree T is a (maximizing) partial order tree if and only if the key at any node is greater than or equal to the keys at each of its • W(n) = n – 1 children (if it has any) e.g. Heaps (or not) Heapsort Strategy • If the elements to be sorted are arranged in a heap, • then we can build a sorted sequence in reverse order by repeatedly removing the element from the root, • rearranging the remaining elements to reestablish the partial order tree property, and so on. • How does it work? Heapsort in action Heapsort Outlines • heapSort(E, n) // Outline 4construct H from E, the set of n elements to be sorted 4for (i = n; i >= 1; i--) f curMax = getMax(H) f deleteMax(H); f E[i] = curMax; • deteleMax(H) // Outline 4copy the rightmost element of the lowest level of H into K 4delete the rightmost element on the lowest level of H 4fixHeap(H, K); 3 Heap construction Strategy Fixheap Outline (divide and conquer) • fixHeap(H, K) // Outline • base case is a tree consisting of one node 4if (H is a leaf) f insert K in root(H); 4else f set largerSubHeap to leftSubtree(H) or rightSubtree(H), whichever has larger key at is root. This involves one key comparison. f if (K.key >= root(largerSubHeap.key) • insert K in root(H); f else • insert root(largerSubHeap) in root(H); • fixHeap(largerSubHeap, K); 4return; • FixHeap requires 2h comparisons of keys in the worst case on a heap with height h. W(n) ≈ 2 lg(n) Construct Heap Outline Heapsort Analysis 4Input: A heap structure H that does not necessarily • The number of comparisons done by fixHeap on heap have the partial order tree property with k nodes is at most 2 lg(k) 4Output: H with the same nodes rearranged to satisfy the 4so the total for all deletions is at most partial order tree property 42 Σ [k=1 to n-1]( lg(k) ) ∈θ(2n lg(n)) • void constructHeap(H) // Outline • Theorem: The number of comparisons of keys done 4if (H is not a leaf) by Heapsort in the worst case is 2n lg(n) + O(n). f constructHeap (left subtree of H); f constructHeap (right subtree of H); • Heapsort does θ(n lg(n)) comparisons on average as f Element K = root(H); well. (How do we know this?) f fixHeap(H, K); 4return; • W(n) = W(n-r-1) + W(r) + 2 lg(n) for n > 1 • W(n) ∈θ(n); heap is constructed in linear time. Implementation issue: storing a tree in an array Accelerated Heapsort • Array E with range from 1, …, n • Speed up Heapsort by about a factor of two. • Suppose the index i of a node is given, then • Normal fixHeap costs 2h comparisons in the worst 4left child has index 2i case. Can we do better? 4right child has index 2i + 1 • … 4parent has index floor( i/2 ) • The solution is a surprising application of divide and • e.g. conquer! 4filter the vacant position halfway down the tree, h/2 4test whether K is bigger than the parent of vacant 4yes: bubble the vacant back up to where K should be 4no: repeat filter the vacant position another halfway down recursively! 4 Accelerated Heapsort Strategy in Action Action continues • K = 55 • K = 55 • nodes not • all shown Accelerated Heapsort Algorithm Algorithm: promote • void fixHeapFast(Element[] E, int n, Element K, int • int promote (Element[] E, int hStop, int vacant, int h) vacant, int h) 4int vacStop; 4if (h <= 1) 4if (h <= hStop) f Process heap of height 0 or 1 f vacStop = vacant; 4else 4else if (E[2*vacant].key <= E[2*vacant+1].key) f int hStop = h/2 f E[vacant] = E[2*vacant+1]; f int vacStop = promoste (E, hStop, vacant, h); f vacStop = promote (E, hStop, 2*vacant+1, h-1) f // vacStop is new vacnt location, at height hStop 4else int vacParent = vacStop / 2; f f E[vacant] = E[2*vacant]; if (E[vacParent].key <= K.key) f f vacStop = promote (E, hStop, 2*vacant, h-1); • E[vacStop] = E[vacParent]; 4return vacStop; • bubbleUpHeap (E, vacant, K, vacParent); f else • fixHeapFast (E, n, K, vacStop, hStop); Algorithm: bubbleUpHeap Analysis: fixHeapFast • void bubbleUpHeap (Element[] E, int root, Element • Essentially, there is one comparison each time vacant K, int vacant) changes a level due to the action of either 4if (vacant == root) bubbleUpHeap or Promote.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us