Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

Total Page:16

File Type:pdf, Size:1020Kb

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.3 QUICKSORT ‣ quicksort ‣ duplicate keys Algorithms ‣ system sorts FOURTH EDITION ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu A classic sorting algorithm: quicksort Critical components in the world’s computational infrastructure. ・Full scientific understanding of their properties has enabled us to develop them into practical system sorts. ・Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Quicksort. ... 2 Quicksort t-shirt 3 2.3 QUICKSORT ‣ quicksort ‣ duplicate keys Algorithms ‣ system sorts ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu Quicksort Basic plan. ・Shuffle the array. ・Partition so that, for some j – entry a[j] is in place – no larger entry to the left of j – no smaller entry to the right of j ・Sort each subarray recursively. input Q U I C K S O R T E X A M P L E shufe K R A T E L E P U I M Q C X O S partitioning item partition E C A I E K L P U T M Q R X O S not greater not less sort left A C E E I K L P U T M Q R X O S sort right A C E E I K L M O P Q R S T U X result A C E E I K L M O P Q R S T U X Quicksort overview 5 number). 9.9 X 10 45 is used to represent infinity. Imaginary conunent I and J are output variables, and A is the array (with valuesTony of x may notHoare be negative and reM values of x may not be subscript bounds M:N) which is operated upon by this procedure. smaller than 1. Partition takes the value X of a random element of the array A, Values of Qd~'(x) may be calculated easily by hypergeometric and rearranges the values of the elements of the array in such a series if x is not too small nor (n - m) too large. Q~m(x) can be way that there exist integers I and J with the following properties : computed from an appropriate set of values of Pnm(X) if X is near M _-< J < I =< NprovidedM < N 1.0 or ix is near 0. Loss of significant digits occurs for x as small as A[R] =< XforM =< R _-< J 1.1 ・if n is largerInvented than 10. Loss of significant quicksort digits is a major diffi-to translateA[R] = RussianXforJ < R < I into English. culty in using finite polynomiM representations also if n is larger A[R] ~ Xfor I =< R ~ N than m. However, QLEG has been tested in regions of x and n The procedure uses an integer procedure random (M,N) which both large and small; chooses equiprobably a random integer F between M and N, and procedure・ [QLEG(m, but nmax,couldn't x, ri, R, Q); value explain In, nmax, x, ri; hisalso a procedurealgorithm exchange, which exchangesor implement the values of its two it! ] real In, mnax, x, ri; real array R, Q; parameters ; begin real t, i, n, q0, s; begin real X; integer F; ・n :=Learned 20; Algol 60 (and recursion).F := random (M,N); X := A[F]; if nmax > 13 then I:=M; J:=N; n := nmax +7; up: for I : = I step 1 until N do ・if Implementedri = 0then quicksort. if X < A [I] then go to down; begin ifm = 0then I:=N; Q[0] := 0.5 X 10g((x + 1)/(x - 1)) down: forJ := J step --1 until M do else if A[J]<X then go to change; begin t := --1.0/sqrt(x X x-- 1); J:=M; q0 := 0; change: if I < J then begin exchange (A[IL A[J]); Tony Hoare Q[O] : = t; I := I+ 1;J:= J - 1; for i : = 1 step 1 until m do go to up 1980 Turing Award begin s := (x+x)X(i-1)Xt end 2: begin 4 × Q[0]+ (3i-i× i-2)× q0; else if [ < F then begin exchange (A[IL A[F]) i if c >= 0 then q0 := Q[0]; I:=I+l 3:begin Q[0] := s end end; end e:= aXe;f := bXd;goto8 if x = 1 then else if F < J tllen begin exchange (A[F], A[J]) ; end 3 ; Q[0] := 9.9 I" 45; J:=J-1 e:=bXc; R[n + 1] := x - sqrt(x X x - 1); end ; ifd ~ 0then for i := n step --1 until 1 do end partition 4: begin R[i] := (i + m)/((i + i + 1) X x f:=bXd; goto8 +(m-i- 1) X R[i+l]); end 4; ALGORITHM 61 go to the end; f:=aXd; goto8 if m = 0 then ALGORITHMPROCEDURES 64FOR RANGE ARITHMETIC 5: end 2; begin if x < 0.5 tben QUICKSORTALLAN GIBB* ifb > 0 then Q[0] := arctan(x) - 1.5707963 else UniversityC. A. R. HOAREof Alberta, Calgary, Alberta, Canada 6: begin Q[0] := - aretan(1/x)end else Elliottbegin Brothers Ltd., Borehamwood, Hertfordshire, Eng. if d > 0 then begin t := 1/sqrt(x X x + 1); begin procedure RANGESUM (a, b, c, d, e, f); q0 := 0; procedure quicksort (A,M,N); value M,N; e := MIN(a X d, b X c); real a,b,c,d,e,f; q[0] := t; array A; integer M,N; f:= MAX(a X c,b X d); go to8 comment The term "range number" was used by P. S. Dwyer, for i : = 2 step 1 until m do comment Quicksort is a very fast and convenient method of end 6; Linear Computations (Wiley, 1951). Machine procedures for begin s := (x + x) X (i -- 1) X t X Q[0I sorting an array in the random-access store of a computer. The e:= bX c; f := aX c; go to8 range arithmetic were developed about 1958 by Ramon Moore, +(3i+iX i -- 2) × q0; entire contents of the store may be sorted, since no extra space is end 5; "Automatic Error Analysis in Digital Computation," LMSD qO := Q[0]; required. The average number of comparisons made is 2(M--N) In f:=aXc; Report 48421, 28 Jan. 1959, Lockheed Missiles and Space Divi- Q[0] := s end end; (N--M), and the average nmnber of exchanges is one sixth this if d _-< O then sion, Palo Alto, California, 59 pp. If a _< x -< b and c ~ y ~ d, R[n + 1] := x - sqrt(x × x + 1); amount. Suitable refinements of this method will be desirable for 7: begin then RANGESUM yields an interval [e, f] such that e =< (x + y) for i := n step - 1 until 1 do its implementation on any actual computer; e:=bXd; goto8 f. Because of machine operation (truncation or rounding) the R[i] := (i + m)/((i -- m + 1) × R[i + 1] begin integer 1,J ; end 7 ; machine sums a -4- c and b -4- d may not provide safe end-points --(i+i+ 1) X x); if M < N then begin partition (A,M,N,I,J); e:=aXd; of the output interval. Thus RANGESUM requires a non-local for i : = 1 step 2 until nmax do quicksort (A,M,J) ; 8: e := ADJUSTPROD (e, -1); real procedure ADJUSTSUM which will compensate for the Rill := -- Rill; quicksort (A, I, N) f := ADJUSTPROD (f, 1) machine arithmetic. The body of ADJUSTSUM will be de- the: for i : = 1 step 1 until nnmx do end end RANGEMPY; pendent upon the type of machine for which it is written and so Q[i] := Q[i - 1] X R[i] end quieksort procedure RANGEDVD (a, b, c, d, e, f) ; is not given here. (An example, however, appears below.) It end QLEG; real a, b, c, d, e, f; is assumed that ADJUSTSUM has as parameters real v and w, comment If the range divisor includes zero the program * This procedure was developed in part under the sponsorship and integer i, and is accompanied by a non-local real procedure exists to a non-local label "zerodvsr". RANGEDVD assumes a of the Air Force Cambridge Research Center. CORRECTION which gives an upper bound to the magnitude CommunicationsALGORITHM 65 of the ACM (July 1961) non-local real procedure ADJUSTQUOT which is analogous of the error involved in the machine representation of a number. FIND (possibly identical) to ADJUSTPROD; The output ADJUSTSUM provides the left end-point of the begin 6 C.output A. R. interval HOARE of RANGESUM when ADJUSTSUM is called if c =< 0 A d ~ 0 then go to zer0dvsr; ALGORITHM 63 Elliottwith i Brothers= --1, and Ltd., the right Borehamwood, end-point when Hertfordshire, called with i Eng.= 1 if c < 0 then PARTITION The procedures RANGESUB, RANGEMPY, and RANGEDVD procedure find (A,M,N,K); value M,N,K; C. A. R. HOARE provide for the remaining fundamental operations in range 1: begin array A; integer M,N,K; ifb > 0 then Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng. arithmetic. RANGESQR gives an interval within which the comment Find will assign to A [K] the value which it would 2: begin square of a range nmnber must lie. RNGSUMC, RNGSUBC, procedure partition (A,M,N,I,J); value M,N; have if the array A [M:N] had been sorted. The array A will be e := b/d; go to3 RNGMPYC and RNGDVDC provide for range arithmetic with array A; integer M,N,1,J; partly sorted, and subsequent entries will be faster than the first; end 2; complex range arguments, i.e.
Recommended publications
  • 2.3 Quicksort
    2.3 Quicksort ‣ quicksort ‣ selection ‣ duplicate keys ‣ system sorts Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · January 30, 2011 12:46:16 PM Two classic sorting algorithms Critical components in the world’s computational infrastructure. • Full scientific understanding of their properties has enabled us to develop them into practical system sorts. • Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Mergesort. last lecture • Java sort for objects. • Perl, C++ stable sort, Python stable sort, Firefox JavaScript, ... Quicksort. this lecture • Java sort for primitive types. • C qsort, Unix, Visual C++, Python, Matlab, Chrome JavaScript, ... 2 ‣ quicksort ‣ selection ‣ duplicate keys ‣ system sorts 3 Quicksort Basic plan. • Shuffle the array. • Partition so that, for some j - element a[j] is in place - no larger element to the left of j j - no smaller element to the right of Sir Charles Antony Richard Hoare • Sort each piece recursively. 1980 Turing Award input Q U I C K S O R T E X A M P L E shu!e K R A T E L E P U I M Q C X O S partitioning element partition E C A I E K L P U T M Q R X O S not greater not less sort left A C E E I K L P U T M Q R X O S sort right A C E E I K L M O P Q R S T U X result A C E E I K L M O P Q R S T U X Quicksort overview 4 Quicksort partitioning Basic plan.
    [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]
  • Sorting Algorithm 1 Sorting Algorithm
    Sorting algorithm 1 Sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation, or reordering, of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds. Classification Sorting algorithms used in computer science are often classified by: • Computational complexity (worst, average and best behaviour) of element comparisons in terms of the size of the list . For typical sorting algorithms good behavior is and bad behavior is .
    [Show full text]
  • Super Scalar Sample Sort
    Super Scalar Sample Sort Peter Sanders1 and Sebastian Winkel2 1 Max Planck Institut f¨ur Informatik Saarbr¨ucken, Germany, [email protected] 2 Chair for Prog. Lang. and Compiler Construction Saarland University, Saarbr¨ucken, Germany, [email protected] Abstract. Sample sort, a generalization of quicksort that partitions the input into many pieces, is known as the best practical comparison based sorting algorithm for distributed memory parallel computers. We show that sample sort is also useful on a single processor. The main algorith- mic insight is that element comparisons can be decoupled from expensive conditional branching using predicated instructions. This transformation facilitates optimizations like loop unrolling and software pipelining. The final implementation, albeit cache efficient, is limited by a linear num- ber of memory accesses rather than the O(n log n) comparisons. On an Itanium 2 machine, we obtain a speedup of up to 2 over std::sort from the GCC STL library, which is known as one of the fastest available quicksort implementations. 1 Introduction Counting comparisons is the most common way to compare the complexity of comparison based sorting algorithms. Indeed, algorithms like quicksort with good sampling strategies [9,14] or merge sort perform close to the lower bound of log n! n log n comparisons3 for sorting n elements and are among the best algorithms≈ in practice (e.g., [24, 20, 5]). At least for numerical keys it is a bit astonishing that comparison operations alone should be good predictors for ex- ecution time because comparing numbers is equivalent to subtraction — an operation of negligible cost compared to memory accesses.
    [Show full text]
  • How to Sort out Your Life in O(N) Time
    How to sort out your life in O(n) time arel Číže @kaja47K funkcionaklne.cz I said, "Kiss me, you're beautiful - These are truly the last days" Godspeed You! Black Emperor, The Dead Flag Blues Everyone, deep in their hearts, is waiting for the end of the world to come. Haruki Murakami, 1Q84 ... Free lunch 1965 – 2022 Cramming More Components onto Integrated Circuits http://www.cs.utexas.edu/~fussell/courses/cs352h/papers/moore.pdf He pays his staff in junk. William S. Burroughs, Naked Lunch Sorting? quicksort and chill HS 1964 QS 1959 MS 1945 RS 1887 quicksort, mergesort, heapsort, radix sort, multi- way merge sort, samplesort, insertion sort, selection sort, library sort, counting sort, bucketsort, bitonic merge sort, Batcher odd-even sort, odd–even transposition sort, radix quick sort, radix merge sort*, burst sort binary search tree, B-tree, R-tree, VP tree, trie, log-structured merge tree, skip list, YOLO tree* vs. hashing Robin Hood hashing https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf xs.sorted.take(k) (take (sort xs) k) qsort(lotOfIntegers) It may be the wrong decision, but fuck it, it's mine. (Mark Z. Danielewski, House of Leaves) I tell you, my man, this is the American Dream in action! We’d be fools not to ride this strange torpedo all the way out to the end. (HST, FALILV) Linear time sorting? I owe the discovery of Uqbar to the conjunction of a mirror and an Encyclopedia. (Jorge Luis Borges, Tlön, Uqbar, Orbis Tertius) Sorting out graph processing https://github.com/frankmcsherry/blog/blob/master/posts/2015-08-15.md Radix Sort Revisited http://www.codercorner.com/RadixSortRevisited.htm Sketchy radix sort https://github.com/kaja47/sketches (thinking|drinking|WTF)* I know they accuse me of arrogance, and perhaps misanthropy, and perhaps of madness.
    [Show full text]
  • Advanced Topics in Sorting  List RSS News Items in Reverse Chronological Order
    Sorting applications Sorting algorithms are essential in a broad variety of applications Organize an MP3 library. Display Google PageRank results. Advanced Topics in Sorting List RSS news items in reverse chronological order. Find the median. Find the closest pair. Binary search in a database. [email protected] Identify statistical outliers. Find duplicates in a mailing list. [email protected] Data compression. Computer graphics. Computational biology. Supply chain management. Load balancing on a parallel computer. http://www.4shared.com/file/79096214/fb2ed224/lect01.html . Sorting algorithms Which algorithm to use? Many sorting algorithms to choose from Applications have diverse attributes Stable? Internal sorts Multiple keys? Insertion sort, selection sort, bubblesort, shaker sort. Quicksort, mergesort, heapsort, samplesort, shellsort. Deterministic? Solitaire sort, red-black sort, splaysort, Dobosiewicz sort, psort, ... Keys all distinct? External sorts Multiple key types? Poly-phase mergesort, cascade-merge, oscillating sort. Linked list or arrays? Radix sorts Large or small records? Distribution, MSD, LSD. 3-way radix quicksort. Is your file randomly ordered? Parallel sorts Need guaranteed performance? Bitonic sort, Batcher even-odd sort. Smooth sort, cube sort, column sort. Cannot cover all combinations of attributes. GPUsort. 1 Case study 1 Case study 2 Problem Problem Sort a huge randomly-ordered file of small Sort a huge file that is already almost in records. order. Example Example Process transaction records for a phone Re-sort a huge database after a few company. changes. Which sorting method to use? Which sorting method to use? 1. Quicksort: YES, it's designed for this problem 1. Quicksort: probably no, insertion simpler and faster 2.
    [Show full text]
  • Sorting Algorithm 1 Sorting Algorithm
    Sorting algorithm 1 Sorting algorithm A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation (reordering) of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2006). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and upper and lower bounds. Classification Sorting algorithms are often classified by: • Computational complexity (worst, average and best behavior) of element comparisons in terms of the size of the list (n). For typical serial sorting algorithms good behavior is O(n log n), with parallel sort in O(log2 n), and bad behavior is O(n2).
    [Show full text]
  • Mergesort and Quicksort
    Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity FOURTH EDITION ‣ comparators ‣ stability ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational infrastructure. ・Full scientific understanding of their properties has enabled us to develop them into practical system sorts. ・Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Mergesort. [this lecture] ... Quicksort. [next lecture] ... 2 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity ‣ comparators ‣ stability ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu Mergesort Basic plan. ・Divide array into two halves. ・Recursively sort each half. ・Merge two halves. input M E R G E S O R T E X A M P L E sort left half E E G M O R R S T E X A M P L E sort right half E E G M O R R S A E E L M P T X merge results A E E E E G L M M O P R R S T X Mergesort overview 4 Abstract in-place merge demo Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. lo mid mid+1 hi a[] E E G M R A C E R T sorted sorted 5 Abstract in-place merge demo Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi].
    [Show full text]
  • Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
    Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity FOURTH EDITION ‣ divide-and-conquer ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Last updated on 9/26/19 4:36 AM Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational infrastructure. Full scientific understanding of their properties has enabled us to develop them into practical system sorts. Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Mergesort. [this lecture] ... Quicksort. [next lecture] ... 2 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity ‣ divide-and-conquer ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Mergesort Basic plan. Divide array into two halves. Recursively sort each half. Merge two halves. input M E R G E S O R T E X A M P L E sort left half E E G M O R R S T E X A M P L E sort right half E E G M O R R S A E E L M P T X merge results A E E E E G L M M O P R R S T X Mergesort overview 4 Abstract in-place merge demo Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. lo mid mid+1 hi a[] E E G M R A C E R T sorted sorted 5 Mergesort: Transylvanian–Saxon folk dance http://www.youtube.com/watch?v=XaqR3G_NVoo 6 Merging: Java implementation private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) { for (int k = lo; k <= hi; k++) copy aux[k] = a[k]; int i = lo, j = mid+1; merge for (int k = lo; k <= hi; k++) { if (i > mid) a[k] = aux[j++]; else if (j > hi) a[k] = aux[i++]; else if (less(aux[j], aux[i])) a[k] = aux[j++]; else a[k] = aux[i++]; } } lo i mid j hi aux[] A G L O R H I M S T k a[] A G H I L M 7 Mergesort quiz 1 How many calls does merge() make to less() in order to merge two sorted subarrays, each of length n / 2, into a sorted array of length n? A.
    [Show full text]
  • Msc THESIS FPGA-Based High Throughput Merge Sorter
    Computer Engineering 2018 Mekelweg 4, 2628 CD Delft The Netherlands http://ce.et.tudelft.nl/ MSc THESIS FPGA-Based High Throughput Merge Sorter Xianwei Zeng Abstract As database systems have shifted from disk-based to in-memory, and the scale of the database in big data analysis increases significantly, the workloads analyzing huge datasets are growing. Adopting FP- GAs as hardware accelerators improves the flexibility, parallelism and power consumption versus CPU-only systems. The accelerators CE-MS-2018-02 are also required to keep up with high memory bandwidth provided by advanced memory technologies and new interconnect interfaces. Sorting is the most fundamental database operation. In multiple- pass merge sorting, the final pass of the merge operation requires significant throughput performance to keep up with the high mem- ory bandwidth. We study the state-of-the-art hardware-based sorters and present an analysis of our own design. In this thesis, we present an FPGA-based odd-even merge sorter which features throughput of 27.18 GB/s when merging 4 streams. Our design also presents stable throughput performance when the number of input streams is increased due to its high degree of parallelism. Thanks to such a generic design, the odd-even merge sorter does not suffer through- put drop for skewed data distributions and presents constant perfor- mance over various kinds of input distributions. Faculty of Electrical Engineering, Mathematics and Computer Science FPGA-Based High Throughput Merge Sorter THESIS submitted in partial fulfillment of the requirements for the degree of MASTER OF SCIENCE in ELECTRICAL ENGINEERING by Xianwei Zeng born in GUANGDONG, CHINA Computer Engineering Department of Electrical Engineering Faculty of Electrical Engineering, Mathematics and Computer Science Delft University of Technology FPGA-Based High Throughput Merge Sorter by Xianwei Zeng Abstract As database systems have shifted from disk-based to in-memory, and the scale of the database in big data analysis increases significantly, the workloads analyzing huge datasets are growing.
    [Show full text]
  • FPGA-Accelerated Samplesort for Large Data Sets
    FPGA-Accelerated Samplesort for Large Data Sets Han Chen, Sergey Madaminov, Michael Ferdman, Peter Milder Stony Brook University {han.chen.2,peter.milder}@stonybrook.edu,{smadaminov,mferdman}@cs.stonybrook.edu ABSTRACT 1 INTRODUCTION Sorting is a fundamental operation in many applications such as Sorting data has always been one of the fundamental computing databases, search, and social networks. Although FPGAs have been operations. In the early days of computing, studies showed that shown very effective at sorting data sizes that fit on chip, systems 25% of all CPU time was spent on sort [13]. Today, application com- that sort larger data sets by shuffling data on and off chipare plexity has increased and data sets have grown to unprecedented bottlenecked by costly merge operations or data transfer time. sizes, with sort remaining a critical component of the dominant dat- We propose a new technique for sorting large data sets, which acenter workloads such as large-scale data warehouses, databases, uses a variant of the samplesort algorithm on a server with a PCIe- and web search. At datacenter scale, there is a constant need for connected FPGA. Samplesort avoids merging by randomly sampling large-scale sorting operations [9, 10], with performance and cost values to determine how to partition data into non-overlapping of running sort being a major consideration of application design. buckets that can be independently sorted. The key to our design is a The popularity of the sort operation has led to the development novel parallel multi-stage hardware partitioner, which is a scalable of many sorting algorithms with different properties, including the high-throughput solution that greatly accelerates the samplesort ubiquitous quicksort [11, 12] and mergesort [5].
    [Show full text]
  • Implementations of Randomized Sorting on Large Parallel Machines
    Implementations of Randomized Sorting on Large Parallel Machines (Preliminary Version) William L. Hightower Jan F. Prins† John H. Reif‡ Elon College University of North Carolina Duke University Elon College NC 27244 Chapel Hill NC 27599-3175 Durham NC 27706 Abstract 1. Introduction Flashsort [RV83,86] and Samplesort [HC83] are Considerable effort has been made by the theoretical related parallel sorting algorithms proposed in the literature. community in the design of parallel algorithms with Both utilize a sophisticated randomized sampling technique excellent and sometimes optimal asymptotic efficiency. to form a splitter set, but Samplesort distributes the splitter However, the field has had less success in demonstrating the set to each processor while Flashsort uses splitter-directed utility of these algorithms by actual implementations. routing. Ideally, the analysis of parallel algorithms should proceed In this paper we present B-Flashsort, a new beyond asymptotic efficiency and be extended to the level of batched-routing variant of Flashsort designed to sort N>P implementations. In practice, this is difficult to do. values using P processors connected in a d-dimensional mesh and using constant space in addition to the input and For one thing, the relation between the asymptotic output. The key advantage of the Flashsort approach over complexity of an algorithm and the performance of an Samplesort is a decrease in memory requirements, by implementation can be considerably more complex for avoiding the broadcast of the splitter set to all processors. implementations of parallel algorithms than for sequential The practical advantage of B-Flashsort over Flashsort is that algorithms. For example, the number of processors or the it replaces pipelined splitter-directed routing with a set of amount of memory available to a processor in existing synchronous local communications and bounds recursion, machines can easily be reached well before the while still being demonstrably efficient.
    [Show full text]