‣ Mergesort ‣ Counting Inversions ‣ Randomized Quicksort ‣ Median and Selection ‣ Closest Pair of Points

Total Page:16

File Type:pdf, Size:1020Kb

‣ Mergesort ‣ Counting Inversions ‣ Randomized Quicksort ‣ Median and Selection ‣ Closest Pair of Points Divide-and-conquer paradigm Divide-and-conquer. 5. DIVIDE AND CONQUER I Divide up problem into several subproblems (of the same kind). Solve (conquer) each subproblem recursively. ‣ mergesort Combine solutions to subproblems into overall solution. ‣ counting inversions Most common usage. ‣ randomized quicksort Divide problem of size n into two subproblems of size n / 2. O(n) time ‣ median and selection Solve (conquer) two subproblems recursively. ‣ closest pair of points Combine two solutions into overall solution. O(n) time Consequence. Brute force: Θ(n2). Lecture slides by Kevin Wayne Divide-and-conquer: O(n log n). Copyright © 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 3/12/18 9:21 AM attributed to Julius Caesar 2 Sorting problem Problem. Given a list L of n elements from a totally ordered universe, 5. DIVIDE AND CONQUER rearrange them in ascending order. ‣ mergesort ‣ counting inversions ‣ randomized quicksort ‣ median and selection ‣ closest pair of points SECTIONS 5.1–5.2 4 Sorting applications Mergesort Obvious applications. Recursively sort left half. Organize an MP3 library. Recursively sort right half. Display Google PageRank results. Merge two halves to make sorted whole. List RSS news items in reverse chronological order. Some problems become easier once elements are sorted. input Identify statistical outliers. A L G O R I T H M S Binary search in a database. Remove duplicates in a mailing list. sort left half A G L O R I T H M S Non-obvious applications. sort right half Convex hull. Closest pair of points. A G L O R H I M S T Interval scheduling / interval partitioning. merge results Scheduling to minimize maximum lateness. A G H I L M O R S T Minimum spanning trees (Kruskal’s algorithm). ... 5 6 Merging Mergesort implementation Goal. Combine two sorted lists A and B into a sorted whole C. Input. List L of n elements from a totally ordered universe. Scan A and B from left to right. Output. The n elements in ascending order. Compare ai and bj. If ai ≤ bj, append ai to C (no larger than any remaining element in B). If ai > bj, append bj to C (smaller than every remaining element in A). MERGE-SORT(L) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ IF (list L has one element) RETURN L. sorted list A sorted list B Divide the list into two halves A and B. 3 7 10 ai 18 2 11 bj 20 23 A ← MERGE-SORT(A). T(n / 2) B ← MERGE-SORT(B). T(n / 2) ERGE Θ(n) merge to form sorted list C L ← M (A, B). RETURN L. 2 3 7 10 11 _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 7 8 A useful recurrence relation Divide-and-conquer recurrence: recursion tree Def. T (n) = max number of compares to mergesort a list of length n. Proposition. If T (n) satisfies the following recurrence, then T (n) = n log2 n. 0 n =1 assuming n Recurrence. T (n)= is a power of 2 2 T (n/2) + n n>1 0 n =1 <latexit sha1_base64="Z3k0jP42WP//6lnqi9e2pn3k2fw=">AAACo3icbVFNa9tAEF0pbZq4H3HSYy5DnYaUFkcyhaSElEAvPfSQFrsJWMKsViNnyWoldkfBrvAP7S0/pStHhdrpwC6PNzNvZt8mpZKWguC35288ebr5bGu78/zFy1c73d29n7aojMCRKFRhrhNuUUmNI5Kk8Lo0yPNE4VVy+6XJX92hsbLQQ5qXGOd8qmUmBSdHTbq/hkf6HURncN5cnSjBqdS1cIp20YnOAjiEiHBGNcgMDrQrCw9gAVE0DvonmMeuZgDRB3Ayx4NG6L2T0etdn/92dSLUaSs/6faCfrAMeAzCFvRYG5eTXW8vSgtR5ahJKG7tOAxKimtuSAqFbt/KYsnFLZ/i2EHNc7RxvTRpAW8dk0JWGHc0wZL9t6PmubXzPHGVOacbu55ryP/lxhVlp3EtdVkRavEwKKsUUAGN45BKg4LU3AEujHS7grjhhgty/7IyZaldolh5ST2rtBRFimusohkZ3rgYrnv2GIwG/U/98PvH3sVpa+cW22dv2BEL2Qm7YF/ZJRsxwe69TW/H6/qH/jf/hz98KPW9tuc1Wwk//gPFucbd</latexit> T (n) ≤ T ( n/2 )+T ( n/2 )+n n>1 <latexit sha1_base64="G9BiiiqVnrc2NLAK0QbOMus1fE8=">AAACy3icbVFdb9MwFHUyPkr56sYjL1d0oCKkLJmQ2mkCTeKFJzSklk2qo8pxbjprjhPZDmoJfeSP8K/4NzhZJtGO++Kjc67P/UpKKYwNwz+ev3fv/oOHvUf9x0+ePns+2D/4ZopKc5zxQhb6MmEGpVA4s8JKvCw1sjyReJFcf2r0i++ojSjU1K5LjHO2VCITnFlHLQa/pyP1FugpUInN06cJLoWqufM0mz49DeENUIsrW4PI4FDBB4gOYQOUzsNgjHnscqYjKjNZFBrU0TFQ3eLG9Z1zbESOQnZaA28ltev98da7T1GlXROLwTAMwjbgLog6MCRdnC/2vQOaFrzKUVkumTHzKCxtXDNtBZfopqoMloxfsyXOHVQsRxPX7TI38NoxKWRumKxQFlr23x81y41Z54nLzJm9MrtaQ/5Pm1c2m8S1UGVlUfGbQlklwRbQXAZSoZFbuXaAcS1cr8CvmGbcuvttVWm9S+Rbk9SrSglepLjDSruymjVbjHZ3dhfMjoOTIPr6fng26dbZIy/JKzIiERmTM/KZnJMZ4V7PC7yxN/G/+Nb/4f+8SfW97s8LshX+r7/FTNWb</latexit> between ⎣n / 2⎦ and n − 1 compares T (n) n = n Solution. T (n) is O(n log2 n). T (n / 2) T (n / 2) 2 (n/2) = n T (n / 4) T (n / 4) T (n / 4) T (n / 4) 4 (n/4) = n Assorted proofs. We describe several ways to solve this recurrence. log 2 n Initially we assume n is a power of 2 and replace ≤ with = in the recurrence. T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) T (n / 8) 8 (n/8) = n ⋮ ⋮ T(n) = n log2 n 9 10 Proof by induction Divide-and-conquer: quiz 1 Proposition. If T (n) satisfies the following recurrence, then T (n) = n log2 n. Which is the exact solution of the following recurrence? 0 n =1 assuming n 0 n =1 T (n)= is a power of 2 T (n)= 2 T (n/2) + n n>1 T ( n/2 )+T ( n/2 )+n 1 n>1 <latexit sha1_base64="Z3k0jP42WP//6lnqi9e2pn3k2fw=">AAACo3icbVFNa9tAEF0pbZq4H3HSYy5DnYaUFkcyhaSElEAvPfSQFrsJWMKsViNnyWoldkfBrvAP7S0/pStHhdrpwC6PNzNvZt8mpZKWguC35288ebr5bGu78/zFy1c73d29n7aojMCRKFRhrhNuUUmNI5Kk8Lo0yPNE4VVy+6XJX92hsbLQQ5qXGOd8qmUmBSdHTbq/hkf6HURncN5cnSjBqdS1cIp20YnOAjiEiHBGNcgMDrQrCw9gAVE0DvonmMeuZgDRB3Ayx4NG6L2T0etdn/92dSLUaSs/6faCfrAMeAzCFvRYG5eTXW8vSgtR5ahJKG7tOAxKimtuSAqFbt/KYsnFLZ/i2EHNc7RxvTRpAW8dk0JWGHc0wZL9t6PmubXzPHGVOacbu55ryP/lxhVlp3EtdVkRavEwKKsUUAGN45BKg4LU3AEujHS7grjhhgty/7IyZaldolh5ST2rtBRFimusohkZ3rgYrnv2GIwG/U/98PvH3sVpa+cW22dv2BEL2Qm7YF/ZJRsxwe69TW/H6/qH/jf/hz98KPW9tuc1Wwk//gPFucbd</latexit> <latexit sha1_base64="VRAYMk/LbRQG3UoGOQDaFFRdfHc=">AAACxXicbVFda9swFJW9ry77SrvHvVyWbqSMZXYYtCNsFPbSxw6StRCZIMvXqagsGUkuCSbsj+yP7d9Mdl1Y0t2nwzn389y0lMK6KPoThA8ePnr8ZO9p79nzFy9f9fcPflpdGY4zrqU2lymzKIXCmRNO4mVpkBWpxIv0+nujX9ygsUKrqVuXmBRsqUQuOHOeWvR/T4fqCL5Cj6a4FKrmvpfd9OgkgvdAHa5cDSKHQ+Vz4kPYAKXzaHSMReJzpkMqc6m1AfVpDNS0+Ajo5AOdQCNyFLLTGngnqY/xbvdvd917FFXWrbHoD6JR1AbcB3EHBqSL88V+cEAzzasCleOSWTuPo9IlNTNOcIn+rspiyfg1W+LcQ8UKtEnd2riBd57JIPfn5Fo5aNl/K2pWWLsuUp9ZMHdld7WG/J82r1x+ktRClZVDxW8H5ZUEp6H5CWTCIHdy7QHjRvhdgV8xw7jzn9ua0vYukW9dUq8qJbjOcIeVbuUMa1yMdz27D2bj0ZdR/OPz4PSks3OPvCFvyZDE5JickjNyTmaEB2EwDOJgHJ6FKnThzW1qGHQ1r8lWhL/+Aje6018=</latexit> − no longer assuming n is a power of 2 Pf. [ by induction on n ] Base case: when n = 1, T(1) = 0 = n log2 n. A. T(n) = n ⎣log2 n⎦ Inductive hypothesis: assume T(n) = n log2 n. B. T(n) = n ⎡log2 n⎤ Goal: show that T(2n) = 2n log2 (2n). ⎣log n⎦ C. T(n) = n ⎣log2 n⎦ + 2 2 − 1 recurrence n ⎡log n⎤ D. T(n) = n ⎡log2 n⎤ − 2 2 + 1 = log k 2 T(2n) = 2 T(n) + 2n <latexit sha1_base64="OIDlLBbcsVZR990sJ0LjHkzac4E=">AAACWXicbVDLSgMxFE3HV62vVpdugkVwUcqMCCoiCG5cVrC20KlDJr2toZlkSO6IZehv+DVu9R8EP8b0sbDVC0lOzrk3N/fEqRQWff+r4K2srq1vFDdLW9s7u3vlyv6j1Znh0ORaatOOmQUpFDRRoIR2aoAlsYRWPLyd6K0XMFZo9YCjFLoJGyjRF5yho6KyH17Ra+q20GZJlA+vg/GTomEtrNFQchDSHXoQndIhDc3kHpWrft2fBv0Lgjmoknk0okphP+xpniWgkEtmbSfwU+zmzKDgEsalMLOQMj5kA+g4qFgCtptPRxvTY8f0aF8btxTSKfu7ImeJtaMkdpkJw2e7rE3I/7ROhv2Lbi5UmiEoPmvUzyRFTSc+0Z4wwFGOHGDcCPdXyp+ZYRydmwtdpm+nwBcmyV8zJbjuwRIr8RUNGzsXg2XP/oLmaf2y7t+fVW8u5nYWySE5IickIOfkhtyRBmkSTt7IO/kgn4Vvz/OKXmmW6hXmNQdkIbyDH4oEtJc=</latexit> k=1 E. Not even Knuth knows. inductive hypothesis = 2 n log2 n + 2n = 2 n (log2 (2n) – 1) + 2n = 2 n log2 (2n). ▪ 11 12 Analysis of mergesort recurrence Digression: sorting lower bound Proposition. If T(n) satisfies the following recurrence, then T(n) ≤ n ⎡log2 n⎤. Challenge. How to prove a lower bound for all conceivable algorithms? 0 n =1 Model of computation. Comparison trees. T (n) ≤ T ( n/2 )+T ( n/2 )+n n>1 Can access the elements only through pairwise comparisons. <latexit sha1_base64="PlaAp4PYCkUIob8GuIy/Wl4iXIM=">AAACynicbVFdb9MwFHUyYKN8deORlys6UBGiJBOiQxNoEi+8IA2pZZPqqHKcm86aY0e2M7WK+sYf4Wfxb3CyTKId98VH51yf+5WWUlgXRX+CcOfe/Qe7ew97jx4/efqsv3/w0+rKcJxyLbW5SJlFKRROnXASL0qDrEglnqdXXxv9/BqNFVpN3KrEpGALJXLBmfPUvP97MlRvgJ4Aldg8PZriQqiae0+77tGTCF4Ddbh0NYgcDhV8hvgQ1kDpbDTGIvEpkyGVudTagHp/BNS0uDF96w0bkaOQndbAW0ltW3+5te5RVFnXw7w/iEZRG3AXxB0YkC7O5vvBAc00rwpUjktm7SyOSpfUzDjBJfqhKosl41dsgTMPFSvQJnW7yzW88kwGuR8m18pBy/77o2aFtasi9ZkFc5d2W2vI/2mzyuXHSS1UWTlU/KZQXklwGprDQCYMcidXHjBuhO8V+CUzjDt/vo0qrXeJfGOSelkpwXWGW6x0S2dYs8V4e2d3wfRo9GkU//gwOD3u1rlHXpCXZEhiMian5Bs5I1PCg93gXfAxGIffQxuuwvomNQy6P8/JRoS//gIlc9Vh</latexit>
Recommended publications
  • Overview Parallel Merge Sort
    CME 323: Distributed Algorithms and Optimization, Spring 2015 http://stanford.edu/~rezab/dao. Instructor: Reza Zadeh, Matriod and Stanford. Lecture 4, 4/6/2016. Scribed by Henry Neeb, Christopher Kurrus, Andreas Santucci. Overview Today we will continue covering divide and conquer algorithms. We will generalize divide and conquer algorithms and write down a general recipe for it. What's nice about these algorithms is that they are timeless; regardless of whether Spark or any other distributed platform ends up winning out in the next decade, these algorithms always provide a theoretical foundation for which we can build on. It's well worth our understanding. • Parallel merge sort • General recipe for divide and conquer algorithms • Parallel selection • Parallel quick sort (introduction only) Parallel selection involves scanning an array for the kth largest element in linear time. We then take the core idea used in that algorithm and apply it to quick-sort. Parallel Merge Sort Recall the merge sort from the prior lecture. This algorithm sorts a list recursively by dividing the list into smaller pieces, sorting the smaller pieces during reassembly of the list. The algorithm is as follows: Algorithm 1: MergeSort(A) Input : Array A of length n Output: Sorted A 1 if n is 1 then 2 return A 3 end 4 else n 5 L mergeSort(A[0, ..., 2 )) n 6 R mergeSort(A[ 2 , ..., n]) 7 return Merge(L, R) 8 end 1 Last lecture, we described one way where we can take our traditional merge operation and translate it into a parallelMerge routine with work O(n log n) and depth O(log n).
    [Show full text]
  • Lecture 16: Lower Bounds for Sorting
    Lecture Notes CMSC 251 To bound this, recall the integration formula for bounding summations (which we paraphrase here). For any monotonically increasing function f(x) Z Xb−1 b f(i) ≤ f(x)dx: i=a a The function f(x)=xln x is monotonically increasing, and so we have Z n S(n) ≤ x ln xdx: 2 If you are a calculus macho man, then you can integrate this by parts, and if you are a calculus wimp (like me) then you can look it up in a book of integrals Z n 2 2 n 2 2 2 2 x x n n n n x ln xdx = ln x − = ln n − − (2 ln 2 − 1) ≤ ln n − : 2 2 4 x=2 2 4 2 4 This completes the summation bound, and hence the entire proof. Summary: So even though the worst-case running time of QuickSort is Θ(n2), the average-case running time is Θ(n log n). Although we did not show it, it turns out that this doesn’t just happen much of the time. For large values of n, the running time is Θ(n log n) with high probability. In order to get Θ(n2) time the algorithm must make poor choices for the pivot at virtually every step. Poor choices are rare, and so continuously making poor choices are very rare. You might ask, could we make QuickSort deterministic Θ(n log n) by calling the selection algorithm to use the median as the pivot. The answer is that this would work, but the resulting algorithm would be so slow practically that no one would ever use it.
    [Show full text]
  • Arxiv:1606.00484V2 [Cs.DS] 4 Aug 2016
    Fast Deterministic Selection Andrei Alexandrescu The D Language Foundation [email protected] Abstract lection algorithm most used in practice [8, 27, 31]. It can The Median of Medians (also known as BFPRT) algorithm, be thought of as a partial QUICKSORT [16]: Like QUICK- although a landmark theoretical achievement, is seldom used SORT,QUICKSELECT relies on a separate routine to divide in practice because it and its variants are slower than sim- the input array into elements less than or equal to and greater ple approaches based on sampling. The main contribution than or equal to a chosen element called the pivot. Unlike of this paper is a fast linear-time deterministic selection al- QUICKSORT which recurses on both subarrays left and right UICKSELECT gorithm QUICKSELECTADAPTIVE based on a refined def- of the pivot, Q only recurses on the side known k inition of MEDIANOFMEDIANS. The algorithm’s perfor- to contain the th smallest element. (The side to choose is mance brings deterministic selection—along with its desir- known because the partitioning computes the rank of the able properties of reproducible runs, predictable run times, pivot.) and immunity to pathological inputs—in the range of prac- The pivot choosing strategy strongly influences QUICK- ticality. We demonstrate results on independent and identi- SELECT’s performance. If each partitioning step reduces the cally distributed random inputs and on normally-distributed array portion containing the kth order statistic by at least a UICKSELECT O(n) inputs. Measurements show that QUICKSELECTADAPTIVE fixed fraction, Q runs in time; otherwise, is faster than state-of-the-art baselines.
    [Show full text]
  • Advanced Topics in Sorting
    Advanced Topics in Sorting complexity system sorts duplicate keys comparators 1 complexity system sorts duplicate keys comparators 2 Complexity of sorting Computational complexity. Framework to study efficiency of algorithms for solving a particular problem X. Machine model. Focus on fundamental operations. Upper bound. Cost guarantee provided by some algorithm for X. Lower bound. Proven limit on cost guarantee of any algorithm for X. Optimal algorithm. Algorithm with best cost guarantee for X. lower bound ~ upper bound Example: sorting. • Machine model = # comparisons access information only through compares • Upper bound = N lg N from mergesort. • Lower bound ? 3 Decision Tree a < b yes no code between comparisons (e.g., sequence of exchanges) b < c a < c yes no yes no a b c b a c a < c b < c yes no yes no a c b c a b b c a c b a 4 Comparison-based lower bound for sorting Theorem. Any comparison based sorting algorithm must use more than N lg N - 1.44 N comparisons in the worst-case. Pf. Assume input consists of N distinct values a through a . • 1 N • Worst case dictated by tree height h. N ! different orderings. • • (At least) one leaf corresponds to each ordering. Binary tree with N ! leaves cannot have height less than lg (N!) • h lg N! lg (N / e) N Stirling's formula = N lg N - N lg e N lg N - 1.44 N 5 Complexity of sorting Upper bound. Cost guarantee provided by some algorithm for X. Lower bound. Proven limit on cost guarantee of any algorithm for X.
    [Show full text]
  • Optimal Node Selection Algorithm for Parallel Access in Overlay Networks
    1 Optimal Node Selection Algorithm for Parallel Access in Overlay Networks Seung Chul Han and Ye Xia Computer and Information Science and Engineering Department University of Florida 301 CSE Building, PO Box 116120 Gainesville, FL 32611-6120 Email: {schan, yx1}@cise.ufl.edu Abstract In this paper, we investigate the issue of node selection for parallel access in overlay networks, which is a fundamental problem in nearly all recent content distribution systems, grid computing or other peer-to-peer applications. To achieve high performance and resilience to failures, a client can make connections with multiple servers simultaneously and receive different portions of the data from the servers in parallel. However, selecting the best set of servers from the set of all candidate nodes is not a straightforward task, and the obtained performance can vary dramatically depending on the selection result. In this paper, we present a node selection scheme in a hypercube-like overlay network that generates the optimal server set with respect to the worst-case link stress (WLS) criterion. The algorithm allows scaling to very large system because it is very efficient and does not require network measurement or collection of topology or routing information. It has performance advantages in a number of areas, particularly against the random selection scheme. First, it minimizes the level of congestion at the bottleneck link. This is equivalent to maximizing the achievable throughput. Second, it consumes less network resources in terms of the total number of links used and the total bandwidth usage. Third, it leads to low average round-trip time to selected servers, hence, allowing nearby nodes to exchange more data, an objective sought by many content distribution systems.
    [Show full text]
  • CS302 Final Exam, December 5, 2016 - James S
    CS302 Final Exam, December 5, 2016 - James S. Plank Question 1 For each of the following algorithms/activities, tell me its running time with big-O notation. Use the answer sheet, and simply circle the correct running time. If n is unspecified, assume the following: If a vector or string is involved, assume that n is the number of elements. If a graph is involved, assume that n is the number of nodes. If the number of edges is not specified, then assume that the graph has O(n2) edges. A: Sorting a vector of uniformly distributed random numbers with bucket sort. B: In a graph with exactly one cycle, determining if a given node is on the cycle, or not on the cycle. C: Determining the connected components of an undirected graph. D: Sorting a vector of uniformly distributed random numbers with insertion sort. E: Finding a minimum spanning tree of a graph using Prim's algorithm. F: Sorting a vector of uniformly distributed random numbers with quicksort (average case). G: Calculating Fib(n) using dynamic programming. H: Performing a topological sort on a directed acyclic graph. I: Finding a minimum spanning tree of a graph using Kruskal's algorithm. J: Finding the minimum cut of a graph, after you have found the network flow. K: Finding the first augmenting path in the Edmonds Karp implementation of network flow. L: Processing the residual graph in the Ford Fulkerson algorithm, once you have found an augmenting path. Question 2 Please answer the following statements as true or false. A: Kruskal's algorithm requires a starting node.
    [Show full text]
  • Dualheap Sort Algorithm: an Inherently Parallel Generalization of Heapsort
    Dualheap Sort Algorithm: An Inherently Parallel Generalization of Heapsort Greg Sepesi Eduneer, LLC [email protected] 2007 June 20 ABSTRACT The proposed dualheap sort algorithm employs the dualheap selection algorithm [3] to recursively partition A generalization of the heapsort algorithm is proposed. subheaps in half as shown in Figure 1-1. When the At the expense of about 50% more comparison and subheaps become too small to partition any more, the move operations for typical cases, the dualheap sort array is sorted. In the figure, the downward triangles algorithm offers several advantages over heapsort: represent the partitions with larger values, the upward improved cache performance, better performance if triangles represent the partitions with smaller values, the input happens to be already sorted, and easier and the arrow represents the direction of increasing parallel implementations. values and increasing heap indices, indicating that the subheaps with smaller values employ negative indices. 1. ALGORITHM OVERVIEW At the expense of about 50% more comparison and A heap is an array with elements regarded as nodes in move operations for the typical case, the dualheap sort algorithm offers significant improvements to heapsort’s a complete binary tree, where node j is the parent of small but important set of inefficiencies: nodes 2j and 2j+1, and where the value of each parent node is superior to the values of its children. This superiority of all the parent nodes is commonly called • When the input is already sorted, dualheap the heap condition. sort performs zero move operations and just NlogN comparison operations. • Heapsort is an elegant and popular sorting algorithm The dualheap sort has better cache by J.W.J.
    [Show full text]
  • Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ
    Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. ! Instructor: X. Zhang Acknowledgement • The set of slides have use materials from the following resources • Slides for textbook by Dr. Y. Chen from Shanghai Jiaotong Univ. • Slides from Dr. M. Nicolescu from UNR • Slides sets by Dr. K. Wayne from Princeton • which in turn have borrowed materials from other resources • Other online resources 2 Outline • Sorting problems and algorithms • Divide-and-conquer paradigm • Merge sort algorithm • Master Theorem • recursion tree • Median and Selection Problem • randomized algorithms • Quicksort algorithm • Lower bound of comparison based sorting 3 Sorting Problem • Problem: Given a list of n elements from a totally-ordered universe, rearrange them in ascending order. CS 477/677 - Lecture 1 4 Sorting applications • Straightforward applications: • organize an MP3 library • Display Google PageRank results • List RSS news items in reverse chronological order • Some problems become easier once elements are sorted • identify statistical outlier • binary search • remove duplicates • Less-obvious applications • convex hull • closest pair of points • interval scheduling/partitioning • minimum spanning tree algorithms • … 5 Classification of Sorting Algorithms • Use what operations? • Comparison based sorting: bubble sort, Selection sort, Insertion sort, Mergesort, Quicksort, Heapsort, … • Non-comparison based sort: counting sort, radix sort, bucket sort • Memory (space) requirement: • in place: require O(1), O(log n) memory • out of place:
    [Show full text]
  • 00 Fast K-Selection Algorithms for Graphics Processing Units
    00 Fast K-selection Algorithms for Graphics Processing Units TOLU ALABI, JEFFREY D. BLANCHARD, BRADLEY GORDON, and RUSSEL STEINBACH, Grinnell College Finding the kth largest value in a list of n values is a well-studied problem for which many algorithms have been proposed. A naive approach is to sort the list and then simply select the kth term in the sorted list. However, when the sorted list is not needed, this method has done quite a bit of unnecessary work. Although sorting can be accomplished efficiently when working with a graphics processing unit (GPU), this article proposes two GPU algorithms, radixSelect and bucketSelect, which are several times faster than sorting the vector. As the problem size grows so does the time savings of these algorithms with a sixfold speedup for float vectors larger than 224 and for double vectors larger than 220, ultimately reaching a 19.1 times speed-up for double vectors of length 228. Categories and Subject Descriptors: D.1.3 [Concurrent Programming]: Parallel programming;; F.2.2 [Non-numerical Algorithms and Problems]: Sorting and searching;; G.4 [Mathematical Software]: Parallel and vector implementations;; I.3.1 [Hardware Architecture]: Graphics Processors General Terms: Algorithms, Design, Experimentation, Performance Additional Key Words and Phrases: K-selection, Order Statistics, Multi-core, Graphics Processing Units, GPGPU, CUDA 1. INTRODUCTION The k-selection problem, a well studied problem in computer science, asks one to find the kth largest value in a list of n elements. This problem is often referred to as finding the kth order statistic. This task appears in numerous applications and our motivating example is a thresholding operation, where only the k largest values in a list are retained while the remaining entries are set to zero.
    [Show full text]
  • Introspective Sorting and Selection Algorithms
    Introsp ective Sorting and Selection Algorithms David R Musser Computer Science Department Rensselaer Polytechnic Institute Troy NY mussercsrpiedu Abstract Quicksort is the preferred inplace sorting algorithm in manycontexts since its average computing time on uniformly distributed inputs is N log N and it is in fact faster than most other sorting algorithms on most inputs Its drawback is that its worstcase time bound is N Previous attempts to protect against the worst case by improving the way quicksort cho oses pivot elements for partitioning have increased the average computing time to o muchone might as well use heapsort which has aN log N worstcase time b ound but is on the average to times slower than quicksort A similar dilemma exists with selection algorithms for nding the ith largest element based on partitioning This pap er describ es a simple solution to this dilemma limit the depth of partitioning and for subproblems that exceed the limit switch to another algorithm with a b etter worstcase bound Using heapsort as the stopp er yields a sorting algorithm that is just as fast as quicksort in the average case but also has an N log N worst case time bound For selection a hybrid of Hoares find algorithm which is linear on average but quadratic in the worst case and the BlumFloydPrattRivestTarjan algorithm is as fast as Hoares algorithm in practice yet has a linear worstcase time b ound Also discussed are issues of implementing the new algorithms as generic algorithms and accurately measuring their p erformance in the framework
    [Show full text]
  • (Deterministic & Randomized): Finding the Median in Linear Time
    Lecture 2 Selection (deterministic & randomized): finding the median in linear time 2.1 Overview Given an unsorted array, how quickly can one find the median element? Can one do it more quickly than by sorting? This was an open question for some time, solved affirmatively in 1972 by (Manuel) Blum, Floyd, Pratt, Rivest, and Tarjan. In this lecture we describe two linear-time algorithms for this problem: one randomized and one deterministic. More generally, we solve the problem of finding the kth smallest out of an unsorted array of n elements. 2.2 The problem and a randomized solution Consider the problem of finding the kth smallest element in an unsorted array of size n. (Let’s say all elements are distinct to avoid the question of what we mean by the kth smallest when we have equalities). One way to solve this problem is to sort and then output the kth element. We can do this in time O(n log n) if we sort using Mergesort, Quicksort, or Heapsort. Is there something faster – a linear-time algorithm? The answer is yes. We will explore both a simple randomized solution and a more complicated deterministic one. The idea for the randomized algorithm is to start with the Randomized-Quicksort algorithm (choose a random element as “pivot”, partition the array into two sets LESS and GREATER consisting of those elements less than and greater than the pivot respectively, and then recursively sort LESS and GREATER). Then notice that there is a simple speedup we can make if we just need to find the kth smallest element.
    [Show full text]
  • Arxiv:1810.12047V1 [Cs.DS] 29 Oct 2018 Es EA21) H Ipicto Sachieved Is Simplification the 2016)
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by The IT University of Copenhagen's Repository Simple and Fast BlockQuicksort using Lomuto’s Partitioning Scheme Martin Aum¨uller Nikolaj Hass October 30, 2018 Abstract worst-case aware quicksort variant called introsort This paper presents simple variants of the Block- [15], Oracle’s Java, which uses a two-pivot quick- Quicksort algorithm described by Edelkamp and sort algorithm [23], and Microsoft’s C#, which uses Weiss (ESA 2016). The simplification is achieved a traditional median-of-three quicksort approach. by using Lomuto’s partitioning scheme instead of In the pursuit of improving the performance Hoare’s crossing pointer technique to partition the of quicksort-based implementations, a long line input. To achieve a robust sorting algorithm that of papers [16, 23, 13, 3, 21] have looked at the works well on many different input types, the paper benefits of introducing more than one pivot in introduces a novel two-pivot variant of Lomuto’s the algorithm. Here, [13, 3, 21] showed that the partitioning scheme. A surprisingly simple twist memory access behavior improves with a small to the generic two-pivot quicksort approach makes number of pivots — an effect that cannot be the algorithm robust. The paper provides an anal- achieved by other more traditional means such as ysis of the theoretical properties of the proposed al- choosing a pivot from a sample of elements [18]. gorithms and compares them to their competitors. However, better cache behavior leaves the problem The analysis shows that Lomuto-based approaches of branch mispredictions unsolved, which is one incur a higher average sorting cost than the Hoare- of the largest bottleneck’s in today’s processor based approach of BlockQuicksort.
    [Show full text]