Tutorial 2: Heapsort, Quicksort, Counting Sort, Radix Sort

Total Page:16

File Type:pdf, Size:1020Kb

Tutorial 2: Heapsort, Quicksort, Counting Sort, Radix Sort Tutorial 2: Heapsort, Quicksort, Counting Sort, Radix Sort Mayank Saksena September 20, 2006 1 Heapsort We review Heapsort, and prove some loop invariants for it. For further information, see Chapter 6 of Introduction to Algorithms. HEAPSORT(A) 1 BUILD-MAX-HEAP(A) Ð eÒg Øh A 2 for i = ( ) downto 2 A i 3 swap A[1] and [ ] ×iÞ e A heaÔ ×iÞ e A 4 heaÔ- ( )= - ( ) 1 5 MAX-HEAPIFY(A; 1) BUILD-MAX-HEAP(A) ×iÞ e A Ð eÒg Øh A 1 heaÔ- ( )= ( ) Ð eÒg Øh A = 2 for i = ( ) 2 downto 1 3 MAX-HEAPIFY(A; i) MAX-HEAPIFY(A; i) i 1 Ð =LEFT( ) i 2 Ö =RIGHT( ) heaÔ ×iÞ e A A Ð > A i Ð aÖ g e×Ø Ð 3 if Ð - ( ) and ( ) ( ) then = i 4 else Ð aÖ g e×Ø = heaÔ ×iÞ e A A Ö > A Ð aÖ g e×Ø Ð aÖ g e×Ø Ö 5 if Ö - ( ) and ( ) ( ) then = i 6 if Ð aÖ g e×Ø = i A Ð aÖ g e×Ø 7 swap A[ ] and [ ] 8 MAX-HEAPIFY(A; Ð aÖ g e×Ø) 1 Loop invariants First, assume that MAX-HEAPIFY(A; i) is correct, i.e., that it makes the subtree with A root i a max-heap. Under this assumption, we prove that BUILD-MAX-HEAP( ) is correct, i.e., that it makes A a max-heap. A We show: at the start of iteration i of the for-loop of BUILD-MAX-HEAP( ) (line ; i ;:::;Ò 2), each of the nodes i +1 +2 is the root of a max-heap. Ò= Ò= ; Ò= ;:::;Ò Initialization Initially, i = 2 . Each node 2 2 +1 is a leaf, and therefore the root of a max-heap (of size 1). i Maintenance Just before iteration i, by the loop invariant, the children of node are i both roots of max-heaps; then MAX-HEAPIFY(A; i) makes the root of a max- > i heap. Thus, also every node j is the root of a max-heap. Hence, the loop invariant is established after i has been decremented. j > Termination When i =0, all nodes 0 are roots of a max-heap; including node 1. Now we argue that Heapsort is correct by proving the following loop invariant: At ::i the start of each iteration of the for-loop (line 2), the subarray A[1 ] is a max-heap A A i ::Ò Ò i containing the i smallest elements of , and the subarray [ +1 ] contains the largest elements of A sorted. Ò A ::Ò Ò Initialization Initially, i = . By line 1, [1 ] is a max-heap containing the A Ò ::Ò smallest (all) elements of A, and the subarray [ +1 ] is empty. ::i i Maintenance Just before some iteration, A[1 ] is a max-heap containing the small- A i ::Ò Ò i est elements of A, and the subarray [ +1 ] contains the largest elements of A sorted, by the loop invariant. A i A i::Ò Swapping A[1] and [ ] preserves the order of elements [ ], and the elements ::i A i::Ò A[1 1] are all smaller than [ ]. A; A ::i After decrementing heaÔ×iÞ e,thecalltoMAX-HEAPIFY( 1) makes [1 1] a max-heap, restoring the loop invariant before next iteration. A :: A Termination When i = 1, [1 1] is a max-heap containing the least element of , ::Ò Ò A and the subarray A[2 ] contains the 1 largest elements of sorted. In other words, A is sorted. 2 Quicksort QUICKSORT(A; Ô; Ö ) < Ö 1 if Ô A; Ô; Ö 2 Õ = PARTITION( ) 3 QUICKSORT(A; Ô; Õ 1) ;Ö 4 QUICKSORT(A; Õ +1 ) 2 PARTITION(A; Ô; Ö ) A Ö 1 Ü = [ ] Ô 2 i = 1 Ô Ö 3 for j = to 1 j Ü 4 if A[ ] i 5 i = +1 i A j 6 swap A[ ] and [ ] i A Ö 7 swap A[ + 1] and [ ] 8 return i +1 A Õ A Ô::Õ PARTITION(A; Ô; Ö ) rearranges and outputs an index such that [ 1] Õ < A Õ ::Ö A[ ] [ +1 ]. The followingproperty is a loop invariant(proof left as an exercise) for any index k : k i A k Ü 1. If Ô , then [ ] k j A k > Ü 2. If i +1 1, then [ ] Ö A k Ü 3. If k = , then [ ]= Performance analysis A; Ô; Ö The choice of pivot element (Ü in PARTITION( )) has a major impact on the performance of Quicksort. We analyze the effect of partitioning informally. The worst possible partitioning would be if we always chose the largest (or small- est) element as pivot – then we get one recursive call of size Ò 1 and one of size 0. Ì Ò Ì Ò Ò Since the partitioning is Θ(Ò), we then get the behaviour: ( )= ( 1)+Θ( ). Ò Ò( +1) 2 Ò Ò Ò ¡¡¡ Ò Expanding, we get Ì ( )=Θ( )+Θ( 1)+ +Θ(1) = Θ 2 = Θ( ). The best partitioningoccurswhen at each recursivecall, the subproblemsare halved, Ì Ò Ì Ò= Ò a ; b ; or: ( )=2 ( 2)+ Θ( ). The Master theorem ( = 2 = 2 log a = 1) gives b Ò Ò Ò Ì ( )=Θ( lg ). Now consider a case when the partitioning always produces a proportional split, Ò Ì «Ò Ì « Ò Ò : < « < e.g.: Ì ( )= ( )+ ((1 ) )+Θ( ), for some constant 0 5 1. Ò « > We can obtain a bound by e.g. drawing a recursion tree for Ì ( ). Since k « Ò «Ò ¡¡¡ « Ò (1 ), the longest path from the root to a leaf is =1. Hence Ò the height of tree is log Ò. For each level there is a Θ( ) term. Additionally, if the 1=« log Ò log 2 1=« 1=« tree was complete, it would have 2 = Ò leaves each with constant cost. Ò Ç Ò Ò All in all we get Ì ( ) = ( lg ). As an exercise, prove this using the substitution method. For further information about Quicksort, see Chapter 7 of Introduction to Algo- rithms. 3 3 Counting sort k Ò k Ç Ò Counting sort sorts integers in [0; k ] in time Θ( + ). If = ( ), it is therefore B C ::k Θ(Ò). Array is for output, and [0 ] for storage. COUNTING-SORT(A; B ; k ) k C i 1 for i =0 to [ ]=0 Ð eÒg Øh A 2 for i =1 to ( ) A i C A i 3 C [ ( )] = [ ( )]+1 k 4 for i =1 to i C i C i 5 C [ ]= [ ]+ [ 1] Ð eÒg Øh A 6 for i = ( ) downto 1 C A i A i 7 B [ [ [ ]]] = [ ] A i C A i 8 C [ [ ]] = [ [ ]] 1 4 Radix sort d d Radix sort assumes that each element of A has digits, numbered 1 to , where 1 is the lowest-order digit. RADIX-SORT(A; d) d 1 for i =1 to i 2 usea stable sort to sort A on digit Definition A sorting algorithm is stable if elements with the same value appear in the same order in the output as in the input. Ò k Counting sort is stable. Using counting sort, radix sort is Θ(d( + )), when sorting d k Ò -digit numbers where each digit can take possible values. 5 Recommended problems Recommended problems from Introduction to Algorithms. I go through some... ¯ Heapsort time bounds: exercise 6.4-3 on p. 136 Ì Ò Ì «Ò Ì « Ò Ò Ç Ò Ò : < ¯ Prove that ( ) = ( )+ ((1 ) )+Θ( ) = ( lg ), where 0 5 < « 1, using e.g. the substitution method ; ; ; ; ; ¯ Quicksort: exercises 7.1- 1 2 3 p. 148, 7.2- 1 3 4 5 p. 153, 7.4-2 p. 159 ; ; ¯ Linear time sorting: exercises 8.2- 2 3 4 p. 170, 8.3-4 p. 173, problem 8-3 p. 179 4.
Recommended publications
  • Radix Sort Comparison Sort Runtime of O(N*Log(N)) Is Optimal
    Radix Sort Comparison sort runtime of O(n*log(n)) is optimal • The problem of sorting cannot be solved using comparisons with less than n*log(n) time complexity • See Proposition I in Chapter 2.2 of the text How can we sort without comparison? • Consider the following approach: • Look at the least-significant digit • Group numbers with the same digit • Maintain relative order • Place groups back in array together • I.e., all the 0’s, all the 1’s, all the 2’s, etc. • Repeat for increasingly significant digits The characteristics of Radix sort • Least significant digit (LSD) Radix sort • a fast stable sorting algorithm • begins at the least significant digit (e.g. the rightmost digit) • proceeds to the most significant digit (e.g. the leftmost digit) • lexicographic orderings Generally Speaking • 1. Take the least significant digit (or group of bits) of each key • 2. Group the keys based on that digit, but otherwise keep the original order of keys. • This is what makes the LSD radix sort a stable sort. • 3. Repeat the grouping process with each more significant digit Generally Speaking public static void sort(String [] a, int W) { int N = a.length; int R = 256; String [] aux = new String[N]; for (int d = W - 1; d >= 0; --d) { aux = sorted array a by the dth character a = aux } } A Radix sort example A Radix sort example A Radix sort example • Problem: How to ? • Group the keys based on that digit, • but otherwise keep the original order of keys. Key-indexed counting • 1. Take the least significant digit (or group of bits) of each key • 2.
    [Show full text]
  • Can We Overcome the Nlog N Barrier for Oblivious Sorting?
    Can We Overcome the n log n Barrier for Oblivious Sorting? ∗ Wei-Kai Lin y Elaine Shi z Tiancheng Xie x Abstract It is well-known that non-comparison-based techniques can allow us to sort n elements in o(n log n) time on a Random-Access Machine (RAM). On the other hand, it is a long-standing open question whether (non-comparison-based) circuits can sort n elements from the domain [1::2k] with o(kn log n) boolean gates. We consider weakened forms of this question: first, we consider a restricted class of sorting where the number of distinct keys is much smaller than the input length; and second, we explore Oblivious RAMs and probabilistic circuit families, i.e., computational models that are somewhat more powerful than circuits but much weaker than RAM. We show that Oblivious RAMs and probabilistic circuit families can sort o(log n)-bit keys in o(n log n) time or o(kn log n) circuit complexity. Our algorithms work in the indivisible model, i.e., not only can they sort an array of numerical keys | if each key additionally carries an opaque ball, our algorithms can also move the balls into the correct order. We further show that in such an indivisible model, it is impossible to sort Ω(log n)-bit keys in o(n log n) time, and thus the o(log n)-bit-key assumption is necessary for overcoming the n log n barrier. Finally, after optimizing the IO efficiency, we show that even the 1-bit special case can solve open questions: our oblivious algorithms solve tight compaction and selection with optimal IO efficiency for the first time.
    [Show full text]
  • CS 758/858: Algorithms
    CS 758/858: Algorithms ■ COVID Prof. Wheeler Ruml Algorithms TA Sumanta Kashyapi This Class Complexity http://www.cs.unh.edu/~ruml/cs758 4 handouts: course info, schedule, slides, asst 1 2 online handouts: programming tips, formulas 1 physical sign-up sheet/laptop (for grades, piazza) Wheeler Ruml (UNH) Class 1, CS 758 – 1 / 25 COVID ■ COVID Algorithms This Class Complexity ■ check your Wildcat Pass before coming to campus ■ if you have concerns, let me know Wheeler Ruml (UNH) Class 1, CS 758 – 2 / 25 ■ COVID Algorithms ■ Algorithms Today ■ Definition ■ Why? ■ The Word ■ The Founder This Class Complexity Algorithms Wheeler Ruml (UNH) Class 1, CS 758 – 3 / 25 Algorithms Today ■ ■ COVID web: search, caching, crypto Algorithms ■ networking: routing, synchronization, failover ■ Algorithms Today ■ machine learning: data mining, recommendation, prediction ■ Definition ■ Why? ■ bioinformatics: alignment, matching, clustering ■ The Word ■ ■ The Founder hardware: design, simulation, verification ■ This Class business: allocation, planning, scheduling Complexity ■ AI: robotics, games Wheeler Ruml (UNH) Class 1, CS 758 – 4 / 25 Definition ■ COVID Algorithm Algorithms ■ precisely defined ■ Algorithms Today ■ Definition ■ mechanical steps ■ Why? ■ ■ The Word terminates ■ The Founder ■ input and related output This Class Complexity What might we want to know about it? Wheeler Ruml (UNH) Class 1, CS 758 – 5 / 25 Why? ■ ■ COVID Computer scientist 6= programmer Algorithms ◆ ■ Algorithms Today understand program behavior ■ Definition ◆ have confidence in results, performance ■ Why? ■ The Word ◆ know when optimality is abandoned ■ The Founder ◆ solve ‘impossible’ problems This Class ◆ sets you apart (eg, Amazon.com) Complexity ■ CPUs aren’t getting faster ■ Devices are getting smaller ■ Software is the differentiator ■ ‘Software is eating the world’ — Marc Andreessen, 2011 ■ Everything is computation Wheeler Ruml (UNH) Class 1, CS 758 – 6 / 25 The Word: Ab¯u‘Abdall¯ah Muh.ammad ibn M¯us¯aal-Khw¯arizm¯ı ■ COVID 780-850 AD Algorithms Born in Uzbekistan, ■ Algorithms Today worked in Baghdad.
    [Show full text]
  • Investigating the Effect of Implementation Languages and Large Problem Sizes on the Tractability and Efficiency of Sorting Algorithms
    International Journal of Engineering Research and Technology. ISSN 0974-3154, Volume 12, Number 2 (2019), pp. 196-203 © International Research Publication House. http://www.irphouse.com Investigating the Effect of Implementation Languages and Large Problem Sizes on the Tractability and Efficiency of Sorting Algorithms Temitayo Matthew Fagbola and Surendra Colin Thakur Department of Information Technology, Durban University of Technology, Durban 4000, South Africa. ORCID: 0000-0001-6631-1002 (Temitayo Fagbola) Abstract [1],[2]. Theoretically, effective sorting of data allows for an efficient and simple searching process to take place. This is Sorting is a data structure operation involving a re-arrangement particularly important as most merge and search algorithms of an unordered set of elements with witnessed real life strictly depend on the correctness and efficiency of sorting applications for load balancing and energy conservation in algorithms [4]. It is important to note that, the rearrangement distributed, grid and cloud computing environments. However, procedure of each sorting algorithm differs and directly impacts the rearrangement procedure often used by sorting algorithms on the execution time and complexity of such algorithm for differs and significantly impacts on their computational varying problem sizes and type emanating from real-world efficiencies and tractability for varying problem sizes. situations [5]. For instance, the operational sorting procedures Currently, which combination of sorting algorithm and of Merge Sort, Quick Sort and Heap Sort follow a divide-and- implementation language is highly tractable and efficient for conquer approach characterized by key comparison, recursion solving large sized-problems remains an open challenge. In this and binary heap’ key reordering requirements, respectively [9].
    [Show full text]
  • Lecture 8.Key
    CSC 391/691: GPU Programming Fall 2015 Parallel Sorting Algorithms Copyright © 2015 Samuel S. Cho Sorting Algorithms Review 2 • Bubble Sort: O(n ) 2 • Insertion Sort: O(n ) • Quick Sort: O(n log n) • Heap Sort: O(n log n) • Merge Sort: O(n log n) • The best we can expect from a sequential sorting algorithm using p processors (if distributed evenly among the n elements to be sorted) is O(n log n) / p ~ O(log n). Compare and Exchange Sorting Algorithms • Form the basis of several, if not most, classical sequential sorting algorithms. • Two numbers, say A and B, are compared between P0 and P1. P0 P1 A B MIN MAX Bubble Sort • Generic example of a “bad” sorting 0 1 2 3 4 5 algorithm. start: 1 3 8 0 6 5 0 1 2 3 4 5 Algorithm: • after pass 1: 1 3 0 6 5 8 • Compare neighboring elements. • Swap if neighbor is out of order. 0 1 2 3 4 5 • Two nested loops. after pass 2: 1 0 3 5 6 8 • Stop when a whole pass 0 1 2 3 4 5 completes without any swaps. after pass 3: 0 1 3 5 6 8 0 1 2 3 4 5 • Performance: 2 after pass 4: 0 1 3 5 6 8 Worst: O(n ) • 2 • Average: O(n ) fin. • Best: O(n) "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems." - Donald Knuth, The Art of Computer Programming Odd-Even Transposition Sort (also Brick Sort) • Simple sorting algorithm that was introduced in 1972 by Nico Habermann who originally developed it for parallel architectures (“Parallel Neighbor-Sort”).
    [Show full text]
  • Counting Sort and Radix Sort
    Counting sort and radix sort Nick Smallbone December 11, 2018 Radix sort is the oldest sorting algorithm which is still in general use; it predates the computer. On the left is Herman Hollerith, the inventor of radix sort. In the late 1800s, he built enormous electromechanical machines which used radix sort to tabulate US census data; he set up a company to sell the machines, and the company later became IBM. On the right you can see a 1950s IBM sorting machine running the radix sort algorithm. Radix sort is unlike the sorting algorithms we have seen so far, in that it is not based on comparisons (≤). However, it only works on certain kinds of data – it is mainly used on numerical data, and is often faster than comparison-based sorting algorithms. Before seeing radix sort, we will look at a simpler algorithm, counting sort. 1 Counting sort #1: sorting a list of digits Suppose you want to sort the first 100 digits of π, in ascending order, by hand. How would you do it? 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 Counting sort is one approach to sorting lists of digits. It can be carried out by hand or on a computer. The idea is like this. First we count how many times each digit occurs in the input data (in this case, the first 100 digits of π): Digit 0 1 2 3 4 5 6 7 8 9 Number of occurrences 8 8 12 12 10 8 9 8 12 14 We can compute this table while looking through the input list only once, by keeping a running total for each digit.
    [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]
  • Sorting Algorithms
    Sorting Algorithms Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics at some website, the presented data has most likely been sorted in some way. In this lecture and in the following lectures we will examine several different ways of sorting. The following are some reasons for investigating several of the different algorithms (as opposed to one or two, or the \best" algorithm). • There exist very simply understood algorithms which, although for large data sets behave poorly, perform well for small amounts of data, or when the range of the data is sufficiently small. • There exist sorting algorithms which have shown to be more efficient in practice. • There are still yet other algorithms which work better in specific situations; for example, when the data is mostly sorted, or unsorted data needs to be merged into a sorted list (for example, adding names to a phonebook). 1 Counting Sort Counting sort is primarily used on data that is sorted by integer values which fall into a relatively small range (compared to the amount of random access memory available on a computer). Without loss of generality, we can assume the range of integer values is [0 : m], for some m ≥ 0. Now given array a[0 : n − 1] the idea is to define an array of lists l[0 : m], scan a, and, for i = 0; 1; : : : ; n − 1 store element a[i] in list l[v(a[i])], where v is the function that computes an array element's sorting value.
    [Show full text]
  • Evaluation of Sorting Algorithms, Mathematical and Empirical Analysis of Sorting Algorithms
    International Journal of Scientific & Engineering Research Volume 8, Issue 5, May-2017 86 ISSN 2229-5518 Evaluation of Sorting Algorithms, Mathematical and Empirical Analysis of sorting Algorithms Sapram Choudaiah P Chandu Chowdary M Kavitha ABSTRACT:Sorting is an important data structure in many real life applications. A number of sorting algorithms are in existence till date. This paper continues the earlier thought of evolutionary study of sorting problem and sorting algorithms concluded with the chronological list of early pioneers of sorting problem or algorithms. Latter in the study graphical method has been used to present an evolution of sorting problem and sorting algorithm on the time line. An extensive analysis has been done compared with the traditional mathematical methods of ―Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort. Observations have been obtained on comparing with the existing approaches of All Sorts. An “Empirical Analysis” consists of rigorous complexity analysis by various sorting algorithms, in which comparison and real swapping of all the variables are calculatedAll algorithms were tested on random data of various ranges from small to large. It is an attempt to compare the performance of various sorting algorithm, with the aim of comparing their speed when sorting an integer inputs.The empirical data obtained by using the program reveals that Quick sort algorithm is fastest and Bubble sort is slowest. Keywords: Bubble Sort, Insertion sort, Quick Sort, Merge Sort, Selection Sort, Heap Sort,CPU Time. Introduction In spite of plentiful literature and research in more dimension to student for thinking4. Whereas, sorting algorithmic domain there is mess found in this thinking become a mark of respect to all our documentation as far as credential concern2.
    [Show full text]
  • 6.006 Introduction to Algorithms Spring 2008
    MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Lecture 11 Sorting IV: Stable Sorting, Radix Sort 6.006 Spring 2008 Lecture 11: Sorting IV: Stable Sorting, Radix Sort Lecture Overview • Stable Sorting • Radix Sort • Quick Sort not officially a part of 6:006 • Sorting Races Stable Sorting Preserves input order among equal elements 4’ 1 3* 4 3 counting sort is stable merge sort is stable 1 3* 3 4’ 4 Figure 1: Stability Selection Sort and Heap: Find maximum element and put it at end of array (swap with element at end of array) NOT STABLE! ← define 3 2a 2b 2b 2a 3 2a <2b Figure 2: Selection Sort Instability Radix Sort • Herman Hollerith card-sorting machine for 1890 census. • Digit by Digit sort by mechanical machine 1. Examine given column of each card in a deck 2. Distribute the card into one of 10 bins 1 Lecture 11 Sorting IV: Stable Sorting, Radix Sort 6.006 Spring 2008 3. Gather cards bin by bin, so cards with first place punched are on top of cards with second place punched, etc. 80 cols . 10 . places . Figure 3: Punch Card MSB vs. LSB? Sort on most significant digit first or least significant digit first? MSB strategy: Cards in 9 of 10 bins must be put aside, leading to a large number of intermediate piles LSB strategy: Can gather sorted cards in bins appropriately to create a deck! Example 3 2 9 7 2 0 7 2 0 3 2 9 4 5 7 3 5 5 3 2 9 3 5 5 6 5 7 4 3 6 4 3 6 4 3 6 8 3 9 4 5 7 8 3 9 4 5 7 4 3 6 6 5 7 3 5 5 6 5 7 7 2 0 3 2 9 4 5 7 7 2 0 3 5 5 8 3 9 6 5 7 8 3 9 Digit sort needs to be stable, else will get wrong result! Figure 4: Example of Radix Sort 2 Lecture 11 Sorting IV: Stable Sorting, Radix Sort 6.006 Spring 2008 Analysis Assume counting sort is auxiliary stable sort.
    [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]
  • Parallel Sorting on Multi-Core Architecture
    Parallel Sorting on Multi-core Architecture A thesis submitted in partial fulfillment of the requirements for the degree of Master of Science By Wei Wang B.S., Zhengzhou University, 2007 2011 Wright State University WRIGHT STATE UNIVERSITY SCHOOL OF GRADUATE STUDIES August 19, 2011 I HEREBY RECOMMEND THAT THE THESIS PREPARED UNDER MY SUPER- VISION BY Wei Wang ENTITLED Parallel Sorting on Multi-core Architecture BE ACCEPTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF Master of Science. Meilin Liu, Ph. D. Thesis Director Mateen Rizki, Ph.D. Department Chair Committee on Final Examination Meilin Liu, Ph. D. Jack Jean, Ph. D. T.K. Prasad, Ph. D. Andrew Hsu, Ph. D. Dean, School of Graduate Studies Copyright c 2011 Wei Wang All Rights Reserved ABSTRACT Wang, Wei. M.S. Department of Computer Science and Engineering, Wright State University, 2011. Parallel Sorting on Multi-Core Architecture. With the limitations given by the power consumption (power wall), memory wall and the instruction level parallelism, the computing industry has turned its direc- tion to multi-core architectures. Nowadays, the multi-core and many-core architectures are becoming the trend of the processor design. But how to exploit these architectures is the primary challenge for the research community. To take advantage of the multi- core architectures, the software design has undergone fundamental changes. Sorting is a fundamental, important problem in computer science. It is utilized in many applications such as databases and search engines. In this thesis, we will in- vestigate and auto-tune two parallel sorting algorithms, i.e., radix sort and sample sort on two parallel architectures, the many-core nVIDIA CUDA enabled graphics proces- sors, and the multi-core Cell Broadband Engine.
    [Show full text]