Arxiv:1810.12047V1 [Cs.DS] 29 Oct 2018 Es EA21) H Ipicto Sachieved Is Simplification the 2016)

Total Page:16

File Type:pdf, Size:1020Kb

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. Moreover, the architectures. analysis is particularly useful to reason about pivot As shown by Brodal and Moruz in [6], mispre- choices that suit the two-pivot approach. An exten- dicting conditional branches, i.e., mispredicting the sive experimental study shows that, despite their outcome of the comparison of two elements of the worse theoretical behavior, the simpler variants per- input, are a necessity in the regime of comparison- form as well as the original version of BlockQuick- efficient sorting algorithms. This means other tools sort. are needed to reduce the performance penalty that these mispredictions incur, which can be as large arXiv:1810.12047v1 [cs.DS] 29 Oct 2018 1 Introduction as 15 cycles on a modern Intel i7 CPU [10], and Sorting a sequence of elements in an efficient way is even larger on other architectures [12]. As we will one of Computer Science’s fundamental problems. describe in Section 2, under certain circumstances It is one of the most important core routines modern processors can avoid branches using so- in many algorithms, and does not seem to lose called conditional move-operations, among others, relevance as we get more and more data to work which were used in a number of implementations on every day. of quicksort variants [17, 8, 7, 4]. More detailed While it has been more than 55 years since the information about these methods is deferred to the quicksort algorithm was described by Hoare in [11], related work part of the introduction. variants of it are still relevant today. This espe- In the branch-free regime of [17, 7, 4], a lot of cially shows when looking at sorting algorithms im- complexity has been introduced to the algorithm. plemented in the standard libraries of modern pro- In fact, Axtmann et al. [4] describe code complex- gramming languages, such as C++, which uses a ity as the main drawback of their approach, stat- ing “formal verification of the correctness of the tice, it is usually chosen as the median in a sample implementation might help to increase trust in the of three elements, while theory suggests to choose remaining cases.” [4, p. 12]. The BlockQuicksort it as the median of Θ(√n) elements to minimize approach of Edelkamp and Weiss [7] has a very el- comparisons [14]. evated code complexity as well, using about 300 Super-Scalar Samplesort Sanders and lines of code for the partitioning procedure. Winkel [17] provided an engineered implemen- The main contribution of this paper are simple, tation of samplesort, a variant of quicksort in fast, and robust variants of branch-free quicksort- which many pivots are used to distribute the based algorithms. Thus, introducing more com- input to buckets. They used conditional moves to plexity is not necessarily required to improve per- traverse the heap-like tree used to find the bucket formance. In more detail, the paper presents sim- of an element. The algorithm uses two phases, pler variants of the BlockQuicksort algorithm de- classification and rearranging elements, and needs scribed in [7]. The difference to BlockQuicksort extra space in the size of the input. lies in utilizing a partitioning scheme that Bent- Tuned Quicksort Elmarsy et al. [8] described ley in [5] attributed to Nico Lomuto, rather than an engineered quicksort variant that uses a branch- using the traditional “crossing pointer” partition- free variant of Lomuto’s partitioning scheme using ing scheme by Hoare as in [7]. The description of conditional moves. the algorithms is provided in Section 3. In there, BlockQuicksort BlockQuicksort as intro- we describe a one-pivot and a two-pivot approach. duced by Edelkamp and Weiss in [7] generalizes Introducing an additional pivot allows us to make Hoare’s partitioning scheme using blocks to handle the algorithm robust and to avoid some of the com- misplaced elements. Filling these two blocks, one mon performance issues of the Lomuto partitioning block for each of the two pointers used in Hoare’s scheme, such as the presence of many equal ele- partitioning scheme, with references to misplaced ments [7]. elements is done branch-free using conditional Theoretical properties of the proposed algo- moves. When these blocks are near-full, elements rithms are discussed in Section 4. The result of are moved into a correct position with regard to this analysis allows us, among others, to reason the pivot choice. When the two pointers are close about good pivot choices for the two-pivot vari- at the end of the partitioning phase, the code has ant. Comparing the Lomuto-based algorithms to to handle many edges cases to produce a correct the Hoare-based approach used in standard Block- partition. To this end, BlockQuicksort uses a Quicksort, we notice a slightly worse comparison “clean-up phase” when partitioning is done. count and a slightly worse memory behavior with Multi-Pivot Quicksort [9, 16, 23, 13, 1, 2, 3, regard to the “scanned elements/memory accesses” 21] studied benefits of using a small number of piv- cost measure discussed in [13, 3, 21]. This makes ots in quicksort. This line of research showed that the results of the experiments in Section 5 quite this approach improves the memory access pattern surprising, which show that the proposed variants drastically [3, 21], an improvement that cannot be can compete with BlockQuicksort. In the same sec- achieved by other means such as choosing a good tion, we speculate about the reasons for this based pivot by sampling input elements. In short, multi- on actual measurements from the CPU. pivot quicksort allows to make fewer accesses to array cells to sort the input, an effect that trans- 1.1 Related Work lates into better running times. The most popular Tuning Quicksort Algorithms Quicksort multi-pivot algorithm is the two-pivot YBB algo- implementations (see Section 2 for a description rithm [23] used in Oracle’s Java since version 7. of the quicksort algorithm) usually use insertion- IPS4o [4] provides an in-place samplesort vari- sort to sort subproblems that are below a certain ant of [17] by combining it with the idea of using size threshold [18]. Moreover, the pivot is usually blocks from BlockQuicksort. Instead of two blocks, chosen from a small sample of elements. In prac- they use a large number of blocks, one for each Algorithm 1 One-Pivot-Quicksort Algorithm 2 Lomuto Partitioning Scheme procedure OnePivotQuicksort(A[1..n]) procedure LomutoPartition(A[1..n]) 1: if n > 1 then 1: p A[n] ← 2: choosePivot(A[1..n]) ⊲ Pivot resides in A[n] 2: i 1 ← 3: i partition(A[1..n]) 3: for j 1; j < n; j j + 1 do ← ← ← 4: OnePivotQuicksort(A[1..i 1]) 4: if A[j] < p then − 5: OnePivotQuicksort(A[i + 1..n]) 5: Swap A[i] and A[j] 6: i i + 1 ← 7: Swap A[i] and A[n] 8: return i bucket. These blocks are filled in a branch-free way using the classification strategy from [17]. All <p p ? ? p elements are moved in blocks and a final rearrange- ≥ · · · ment step is needed to swap blocks to correct po- i↑ j↑ sitions. Again, a lot of care is needed in the clean- Figure 1: Lomuto invariant: A[1..i 1] consists 4 − up phase. IPS o lends itself to parallelization, as of elements smaller than p, A[i..j 1] consists of − demonstrated by the experiments in [4]. elements at least as large as p; A[j..n 1] has not − been looked at, which is depicted by filling this part 1.2 Our Contributions This paper contains of the array with both colors. the following contributions: We present variants of BlockQuicksort using • Lomuto’s partitioning scheme (Algorithm 4, Lomuto’s Partitioning Scheme Tradition- Algorithm 5) and evaluate them experimen- ally, one uses the crossing pointer technique de- tally. Comparing the implementations, the scribed by Hoare [11] to partition the input.
Recommended publications
  • 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]
  • (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]
  • Fast Deterministic Selection
    Fast Deterministic Selection Andrei Alexandrescu The D Language Foundation, Washington, USA Abstract The selection problem, in forms such as finding the median or choosing the k top ranked items in a dataset, is a core task in computing with numerous applications in fields as diverse as statist- ics, databases, Machine Learning, finance, biology, and graphics. The selection algorithm Median of Medians, although a landmark theoretical achievement, is seldom used in practice because it is slower than simple approaches based on sampling. The main contribution of this paper is a fast linear-time deterministic selection algorithm MedianOfNinthers based on a refined definition of MedianOfMedians. A complementary algorithm MedianOfExtrema is also proposed. These algorithms work together to solve the selection problem in guaranteed linear time, faster than state-of-the-art baselines, and without resorting to randomization, heuristics, or fallback approaches for pathological cases. We demonstrate results on uniformly distributed random numbers, typical low-entropy artificial datasets, and real-world data. Measurements are open- sourced alongside the implementation at https://github.com/andralex/MedianOfNinthers. 1998 ACM Subject Classification F.2.2 Nonnumerical Algorithms and Problems Keywords and phrases Selection Problem, Quickselect, Median of Medians, Algorithm Engin- eering, Algorithmic Libraries Digital Object Identifier 10.4230/LIPIcs.SEA.2017.24 1 Introduction The selection problem is widely researched and has numerous applications. Selection is finding the kth smallest element (also known as the kth order statistic): given an array A of length |A| = n, a non-strict order ≤ over elements of A, and an index k, the task is to find the element that would be in slot A[k] if A were sorted.
    [Show full text]
  • SI 335, Unit 7: Advanced Sort and Search
    SI 335, Unit 7: Advanced Sort and Search Daniel S. Roche ([email protected]) Spring 2016 Sorting’s back! After starting out with the sorting-related problem of computing medians and percentiles, in this unit we will see two of the most practically-useful sorting algorithms: quickSort and radixSort. Along the way, we’ll get more practice with divide-and-conquer algorithms and computational models, and we’ll touch on how random numbers can play a role in making algorithms faster too. 1 The Selection Problem 1.1 Medians and Percentiles Computing statistical quantities on a given set of data is a really important part of what computers get used for. Of course we all know how to calculate the mean, or average of a list of numbers: sum them all up, and divide by the length of the list. This takes linear-time in the length of the input list. But averages don’t tell the whole story. For example, the average household income in the United States for 2010 was about $68,000 but the median was closer to $50,000. Somehow the second number is a better measure of what a “typical” household might bring in, whereas the first can be unduly affected by the very rich and the very poor. Medians are in fact just a special case of a percentile, another important statistical quantity. Your SAT scores were reported both as a raw number and as a percentile, indicating how you did compared to everyone else who took the test. So if 100,000 people took the SATs, and you scored in the 90th percentile, then your score was higher than 90,000 other students’ scores.
    [Show full text]
  • Basic Introduction Into Algorithms and Data Structures
    Lecture given at the International Summer School Modern Computational Science (August 15-26, 2011, Oldenburg, Germany) Basic Introduction into Algorithms and Data Structures Frauke Liers Computer Science Department University of Cologne D-50969 Cologne Germany Abstract. This chapter gives a brief introduction into basic data structures and algorithms, together with references to tutorials available in the literature. We first introduce fundamental notation and algorithmic concepts. We then explain several sorting algorithms and give small examples. As fundamental data structures, we in- troduce linked lists, trees and graphs. Implementations are given in the programming language C. Contents 1 Introduction ........................... 2 2 Sorting .............................. 2 2.1 InsertionSort.......................... 3 2.1.1 Some Basic Notation and Analysis of Insertion Sort . 4 2.2 Sorting using the Divide-and-Conquer Principle . 7 2.2.1 Merge Sort . 7 2.2.2 Quicksort ........................... 9 2.3 A Lower Bound on the Performance of Sorting Algorithms 12 3 Select the k-thsmallestelement . 12 4 BinarySearch .......................... 13 5 Elementary Data Structures . 14 5.1 Stacks.............................. 15 1 Algorithms and Data Structures (Liers) 5.2 LinkedLists........................... 17 5.3 Graphs, Trees, and Binary Search Trees . 18 6 AdvancedProgramming . 21 6.1 References............................ 21 1 Introduction This chapter is meant as a basic introduction into elementary algorithmic principles and data structures used in computer science. In the latter field, the focus is on processing information in a systematic and often automatized way. One goal in the design of solution methods (algorithms) is about making efficient use of hardware resources such as computing time and memory. It is true that hardware develop- ment is very fast; the processors’ speed increases rapidly.
    [Show full text]
  • Selection (Deterministic & Randomized): Finding the Median in Linear Time
    Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.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. 4.2 The problem and a randomized solution A related problem to sorting is the problem of finding the kth smallest element in an unsorted array. (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. 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 notice that in Randomized-Quicksort, after the par- titioning step we can tell which subarray has the item we are looking for, just by looking at their sizes. So, we only need to recursively examine one subarray, not two. For instance, if we are looking for the 87th-smallest element in our array, and after partitioning the “LESS” subarray (of elements less than the pivot) has size 200, then we just need to find the 87th smallest element in LESS.
    [Show full text]
  • 1.6 Median Selection
    Algorithms Lecture 1: Recursion [Fa’10] 1.6 Median Selection So how do we find the median element of an array in linear time? The following algorithm was discovered by Manuel Blum, Bob Floyd, Vaughan Pratt, Ron Rivest, and Bob Tarjan in the early 1970s. Their algorithm actually solves the more general problem of selecting the kth largest element in an array, using the following recursive divide-and-conquer strategy. The subroutine PARTITION is the same as the one used in QUICKSORT. SELECT(A[1 .. n], k): if n 25 ≤use brute force else m n=5 for i d 1 toe m B [i] SELECT(A[5i 4 .. 5i], 3) Brute force! mom S ELECT(B[1 .. m],−m=2 ) hhRecursion! ii b c hh ii r PARTITION(A[1 .. n], mom) if k < r return SELECT(A[1 .. r 1], k) Recursion! else if k > r − hh ii return SELECT(A[r + 1 .. n], k r) Recursion! else − hh ii return mom If the input array is too large to handle by brute force, we divide it into n=5 blocks, each containing exactly 5 elements, except possibly the last. (If the last block isn’t full, justd throwe in a few s.) We find the median of each block by brute force and collect those medians into a new array. Then we1 recursively compute the median of the new array (the median of medians — hence ‘mom’) and use it to partition the input array. Finally, either we get lucky and the median-of-medians is the kth largest element of A, or we recursively search one of the two subarrays.
    [Show full text]
  • Worst-Case Efficient Sorting with Quickmergesort
    Worst-Case Efficient Sorting with QuickMergesort Stefan Edelkamp1 and Armin Weiß2 1King's College London, UK 2FMI, Universit¨atStuttgart, Germany San Diego, January 7, 2019 25 20 [ns] n log 15 n 10 time per 5 Right: random with2 large10 elements215 in220 the middle225 and end. number of elements n 25 20 [ns] n log 15 n 10 time per 5 210 215 220 225 number of elements n std::sort (Quicksort/Introsort) std::partial sort (Heapsort) std::stable sort (Mergesort) Running times (divided by n log n) for sorting integers. Left: random inputs. Comparison-based sorting: Quicksort, Heapsort, Mergesort 25 20 [ns] n log 15 n 10 time per 5 Right: random with2 large10 elements215 in220 the middle225 and end. number of elements n Comparison-based sorting: Quicksort, Heapsort, Mergesort 25 20 [ns] n log 15 n 10 time per 5 210 215 220 225 number of elements n std::sort (Quicksort/Introsort) std::partial sort (Heapsort) std::stable sort (Mergesort) Running times (divided by n log n) for sorting integers. Left: random inputs. Comparison-based sorting: Quicksort, Heapsort, Mergesort 25 25 20 20 [ns] [ns] n n log log 15 15 n n 10 10 time per time per 5 5 210 215 220 225 210 215 220 225 number of elements n number of elements n std::sort (Quicksort/Introsort) std::partial sort (Heapsort) std::stable sort (Mergesort) Running times (divided by n log n) for sorting integers. Left: random inputs. Right: random with large elements in the middle and end. Wish to have three times 3: Make Quicksort worst-case efficient: Introsort, median-of-medians pivot selection Make Heapsort
    [Show full text]
  • 3 Prune-And-Search Is Even
    3 Prune-and-Search is even. The expected running time increases with increas- ing number of items, T (k) ≤ T (m) if k ≤ m. Hence, We use two algorithms for selection as examples for the n 1 prune-and-search paradigm. The problem is to find the T (n) ≤ n + max{T (m − 1),T (n − m)} n i-smallest item in an unsorted collection of n items. We mX=1 n could first sort the list and then return the item in the i-th 2 position, but just finding the i-th item can be done faster ≤ n + T (m − 1). n n m= +1 than sorting the entire list. As a warm-up exercise consider X2 selecting the 1-st or smallest item in the unsorted array Assume inductively that T (m) ≤ cm for m< n and A[1..n]. a sufficiently large positive constant c. Such a constant c can certainly be found for m = 1, since for that case min =1; the running time of the algorithm is only a constant. This for j =2 to n do establishes the basis of the induction. The case of n items if A[j] < A[min] then min = j endif reduces to cases of m<n items for which we can use the endfor. induction hypothesis. We thus get n The index of the smallest item is found in n − 1 com- 2c T (n) ≤ n + m − 1 parisons, which is optimal. Indeed, there is an adversary n n m=X+1 argument, that is, with fewer than n − 1 comparisons we 2 c n can change the minimum without changing the outcomes = n + c · (n − 1) − · +1 of the comparisons.
    [Show full text]
  • (Deterministic & Randomized): Finding the Median in Linear Time
    Lecture 4 Selection (deterministic & randomized): finding the median in linear time 4.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 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 give linear-time algorithms for the problem of finding the kth smallest out of an unsorted array of n elements. 4.2 Lower bounds for comparison-based sorting We saw in the last lecture that Randomized Quicksort will sort any array of size n with only O(n log n) comparisons in expectation. Mergesort and Heapsort are two other algorithms that will sort any array of size n with only O(n log n) comparisons (and these are deterministic algorithms, so there is no “in expectation”). Can one hope to sort with fewer, i.e., with o(n log n) comparisons? Let us quickly see why the answer is “no”, at least for deterministic algorithms (we will analyze lower bounds for randomized algorithms later). To be clear, we are considering comparison-based sorting algorithms that only operate on the input array by comparing pairs of elements and moving elements around based on the results of these comparisons. Sometimes it is helpful to view such algorithms as providing instructions to a data- holder (“move this element over here”, “compare this element with that element and tell me which is larger”). The two key properties of such algorithms are: 1.
    [Show full text]
  • ‣ 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.
    [Show full text]
  • Medians and Selection (Tuesday, Feb 24, 1998) Read: Todays Material Is Covered in Sections 10.2 and 10.3
    Lecture Notes CMSC 251 Finally, in the recurrence T (n)=4T(n=3) + n (which corresponds to Case 1), most of the work is done at the leaf level of the recursion tree. This can be seen if you perform iteration on this recurrence, the resulting summation is log n X3 4 i n : 3 i=0 (You might try this to see if you get the same result.) Since 4=3 > 1, as we go deeper into the levels of the tree, that is deeper into the summation, the terms are growing successively larger. The largest contribution will be from the leaf level. Lecture 9: Medians and Selection (Tuesday, Feb 24, 1998) Read: Todays material is covered in Sections 10.2 and 10.3. You are not responsible for the randomized analysis of Section 10.2. Our presentation of the partitioning algorithm and analysis are somewhat different from the ones in the book. Selection: In the last couple of lectures we have discussed recurrences and the divide-and-conquer method of solving problems. Today we will give a rather surprising (and very tricky) algorithm which shows the power of these techniques. The problem that we will consider is very easy to state, but surprisingly difficult to solve optimally. Suppose that you are given a set of n numbers. Define the rank of an element to be one plus the number of elements that are smaller than this element. Since duplicate elements make our life more complex (by creating multiple elements of the same rank), we will make the simplifying assumption that all the elements are distinct for now.
    [Show full text]