Comparison Sort Comparison Sort

Comparison Sort Comparison Sort

' $ ComparisonComparison Sort Sort A Comparison Sort is a sorting algorithm where the final order is determined only by comparisons between the input elements. - In Insertion Sort, the proper position to insert the current element is found by comparing the current element with the elements in the sorted sub-array. - In Heap Sort, the Heapify procedure determines where to place items based on their comparisons with adjacent items (parent-child) in the tree. - In Merge Sort, the merge procedure chooses an item from one of two arrays after comparing the top items from both arrays. - In Quicksort, the Partition procedure compares each item of the subarray, one by one, to the pivot element to determine whether or not to swap it with another element. CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 1 ' $ SummarySummary for for Comparison Comparison Sort Sort Algorithms Algorithms Sorting Worst Best Average Methods Case Case Case Applications InsertionSort n2 n n2 Very fast when n< 50 MergeSort nlgn nlgn nlgn Need extra space; good for linked lists. HeapSort nlgn nlgn nlgn Good for real-time appl. QuickSort n2 nlgn nlgn Practical and fast CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 2 ' $ DecisionDecision Tree Tree Model Model - Each comparison sort algorithm can be viewed abstractly in terms of a decision tree. - It is a rooted binary tree where internal nodes represent comparisons between two keys and leaves represent sorted outputs. A comparison Sort algorithm + an input size n ↔ a decision tree. CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 3 ' $ InsertionInsertion Sort Sort Algorithm Algorithm INSERTION-SORT(A) 1 for j:=2 to length of A do 2 key := A[j] 3 /* put A[j] into A[1..j-1] */ 4 i:=j-1 5 while( i> 0 AND A[i] > key) 6 A[i+1] := A[i] 7 i:=i-1 8 A[i+1] := key CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 4 ' $ TheThe Decision Decision Tree Tree Corresponding Corresponding to to Insertion Insertion SortSort (n (n = = 3) 3) b:a a b c a <= b a > b First Iteration a, b b, a c:b c:a b <= c b > c a <= c a > c Second Iteration a, b, c c:a b, a, c c:b a <= c a > c b <= c b > c a, c, b c, a, b b, c, a c, b, a CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 5 ' $ AnotherAnother Example: Example: Bubble Bubble Sort Sort Bubble elements to their proper place in the array by comparing elements i and i + 1, and swapping if A[i] >A[i + 1]. - last position has the largest element (loop invariant). - then bubble every element except the last one towards its correct position. - then repeat until done or until the end of quarter. - whichever comes first ... CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 6 ' $ IllustrationIllustration of of Bubble Bubble Sort Sort Input: 4253 4 2 5 3 2 4 5 3 2453 2 4 3 5 2 4 3 5 2 4 3 5 2 3 4 5 2 3 4 5 2 345 2345 Another input: 321 3 2 1 2 3 1 2 1 3 123 CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 7 ' $ TheThe Decision Decision Tree Tree Corresponding Corresponding to to Bubble Bubble SortSort (n (n = = 3) 3) a:b a b c a <= b a > b First Iteration b:c a:c b <= c b > c a <= c a > c a, c, b b, c, a a, b, c a:c b, a, c b:c a <= c a > c b <= c b > c Second Iteration a, c, b c, a, b b, c, a c, b, a CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 8 ' $ LowerLower Bound Bound for for Comparison-based Comparison-based Sorting Sorting The decision trees of comparison-based sorting algorithms: - Each internal node contains a comparison. - Each leaf contains a permutation. All the leaf nodes produce a correctly sorted sequence. - Algorithm execution = a path from the root to a leaf. - Worst-case number of comparisons = height of tree. - Idea: If we find a lower bound on the height of the decision tree, we will have a lower bound on the running time of any comparison-based sorting algorithm. CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 9 ' $ AA Necessary Necessary Condition Condition for for Any Any Correct Correct ComparisonComparison Sorting Sorting Algorithm Algorithm Given an input size n, there are n! possible orderings of the elements, so the corresponding decision tree needs to have at least n! leaf nodes to produce these permutations. a b c CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 10 ' $ LowerLower Bound Bound on on the the Height Height of of a a Decision Decision TreeTree Theorem: Any decision tree that sorts n elements has height Ω(nlgn). - Suppose the decision tree has height h. - A binary tree of height h has at most 2h leaves. - The decision tree must have at least n! leaves, hence: 2h ≥ l ≥ n! ⇒ h ≥ lg(n!). - Claim: lg(n!) = Θ(nlgn) (see hw 1). - Therefore h ≥ Θ(nlgn) ⇒ h = Ω(nlgn). CS404/504 Computer Science & % Design and Analysis of Algorithms: Lecture 9 11.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 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