Sort-New.Pdf

Total Page:16

File Type:pdf, Size:1020Kb

Sort-New.Pdf 1 FAST EXCHANGE SORTS Nachum Dershowitz Hon-Wai Leong Department of Computer Science Department of Computer Science University of Illinois National University of Singap ore Urbana, IL 61801, USA Singap ore 0511 Abstract We present three variations of the following new sorting theme: Throughout the sort, the arrayismaintained in piles of sorted ele- ments. Ateach step, the piles are split into two parts, so that the elements of the left piles are smaller than or equal to the elements of the right piles. Then, the two parts are each sorted, recursively. The theme, then, is a combination of Hoare's Quicksort idea, and the Pick algorithm, by Blum, et al., for linear selection. The variations arise from the p ossible choices of splitting metho d. Twovariations attempt to minimize the average numb er of com- parisons. The b etter of these has an average p erformance of 1:075n lg n comparisons. The third variation sacri ces the average case for a worst-case p erformance of 1:756n lg n, which is b etter than Heapsort. They all require minimal extra space and ab out as manydatamoves as comparisons. 1 Intro duction The sorting problem is: Given an array a ;a ; ;a of elements, rearrange 1 2 n them so that a a a , where is a given linear ordering of 1 2 n elements. An exchange sort [Knut73] is one that go es ab out this task by rep eatedly lo oking for a pair of elements a and a 1 i<j nthat i j are inverted a >a and exchanging them. By combining ideas from Hoare's i j Quicksort algorithm [Hoar61, Hoar62] and Blum, Floyd, Pratt, Rivest, and Tarjan's Pick algorithm [BlFP73] for linear selection, wehave come up with a new scheme for exchange sorts. One the one hand, wewere interested in the p ossibilityofimproving the average case of Quicksort, and, on the other hand, in improving the 1 This researchwas supp orted in part by the National Science Foundation under Grant DCR 85-13417. 1 worst case of Heapsort. The mo del under consideration is a comparison- based mo del; the cost of a metho d is measured by the numb er of compar- isons required for sorting n elements. It is well-known [Knut73] that under this comparison-based mo del, sorting has cost n lg n. Furthermore, there are well-known metho ds that are optimal to within a multiplicativecon- stant. Heapsort [Will64] has b oth average- and worst-case p erformances of O n lg n; asymptotically,itsworst case is 2n lg n. Quicksort [Hoar61] has average-case p erformance of 1:39n lg n [Knut73]. This was improved byS- ingleton [Sing69] to 1:19n lg n using the median-of-three metho d, and can b e made arbitrarily close to n lg n by increasing the sample size. However, Quicksort su ers from quadratic worst-case p erformance. In b oth Heapsort and Quicksort, the numb er of data-moves is the same order of magnitude as the numb er of comparisons. Binary-Insertion Sort [Knut73] is also optimal for this mo del, since it has worst-case p erformance of O n lg n comparisons; however, it requires O nsup2 data-moves. Brie y,ourscheme is as follows: The arrayismaintained in small piles of sorted elements throughout the sorting pro cess. Thus, the rst step is to prepro cess the input arrayinto piles of sorted elements. This step is done only once; from then on, the piled structure is preserved. Ateach subsequent step, a particular pivot elementischosen and the piles are split into two parts so that all the elements in the left piles are smaller than all the elements in the right piles. Then, the two parts are each sorted, recursively. The theme, then, whichwe will call Pilesort,combines splitting around a pivot, as in Quicksort, with piling to nd a pivot,asinPick. The variations we consider arise from di erent p ossibilities for splitting. Twovariations attempt to minimize the average numb er of comparisons over all p ossible input array p ermutations; another sacri ces average-case p er- formance for an O n lg nworst-case that out-p erforms Heapsort [Will64, Floy64]. They all require some extra space and ab out as many data moves as comparisons; their practical value is limited to cases where comparisons are relatively exp ensive. In the next section, we elab orate on the general scheme. Section 3 ana- lyzes its time complexity. Three variations of the scheme are considered in Sections 4 and 5; exp erimental results are given in Section 6. We conclude with a brief discussion. 2 2 The Theme The rst step is to prepro cess the input array, creating piles of three elements, each pile sorted. The result is shown in Figure 1, assuming for simplicity that n is a multiple of 3. This step costs at most n comparisons; on the aver- age, it costs 8n=9. From this p oint on, the piled structure will b e maintained by the algorithm. To sort a piled array,apivot-pile is rst selected, and its middle is used as the pivot element . Then the piles themselves are rearranged so that the middle elements of the left-piles are less than or equal to the pivot element, while the middle elements of the right-piles are greater than or equal to the pivot element. This is similar to Hoare's Partition except that key compar- isons are based on the pile middles and that entire piles are b eing moved around. Di erentways of cho osing the pivot lead to di erent algorithms, as will b e describ ed in subsequent sections. We shall refer to this rearrangement as Pile-Partition .AfterPile-Partition , the situation is as depicted in Figure 2. At this p oint, note that elements in W [ Z are on the prop er side of the partition, while elements in X [ Y may b e greater or smaller than the pivot element. To complete the partitioning pro cess, two things need to b e done: a The elements in X [ Y must b e compared with the pivot elementtode- termine if they are in the prop er partition. This step takes n=3 comparisons. b Restoring the pile structure. In order to eciently restore the pile structure, we note the following: i A loose element can b e merged with a pile of two sorted elements in 2 comparisons at most, and 5/3 comparisons on the average. ii Three lo ose elements can b e repiled in 3 comparisons at most, and 8/3 comparisons on the average. iii Three piles of two elements can b e merged into two piles of three elements by rst comparing two maximal elements to form one pile of three, and then comparing the remaining lo ose element with the smaller, and, if necessary, the larger element of the remaining pile of two. This takes 3 comparisons at most, and 232/90 comparisons on the average the average is over all p ossible arrangements of three piles of twoelements. The following pro cedures sketch the whole sorting scheme: procedure Sort; 3 begin Initial-Piling 1,N; Pilesort 1,N end; procedure Pilesort L, R ; begin if R - L <= Threshold then Smallsort L,R else begin Pile-Partition L,R,T; Partition-Cleanup L,R,T; Pilesort L, T-1; Pilesort T+1, R end end; As in Quicksort [Knut73], a Smal lsort routine is used whenever the array segment is so small that the overhead do es not justify recursive calls. The Partition-Cleanup pro cedure do es tasks a and b simultaneously. It is similar to the Partition phase of Quicksort and is applied to the X and Y elements. The elements in X are scanned until an element that is greater than the pivot i.e. in the wrong partition is found; then the Y -elements are scanned until one smaller than the pivot is encountered. These elements are broken o from their resp ective piles, interchanged, and inserted into the remaining piles of two. By i ab ove, this takes at most 2 comparison p er elementmoved and 5/3 comparisons on the average. This pro cess is continued until all the elements of either X or Y are exhausted. Without loss of generality, assume that the elements in Y are exhausted. At this p oint, the remaining elements in X are scanned until three elements are found out of place. These form a pile that is moved to the right partition and the remaining remaining three piles of two are repiled into two piles of three. This will take, in the worst-case, 2 comparisons p er elementmoved, and 8=3 + 232=90 =3 = 472/270 comparisons p er elementmoved, on the average. To summarize, Partition-Cleanup takes n comparisons in the worst case, 4 n=3 to decide whether the element is in the prop er partition and 2n=3 for the actual moves. The average case, however, dep ends on how manyelements get moved and on how they are moved. Finally, once the piles are completely partitioned, the algorithm recur- sively sorts each of the two parts. 3 Analysis Let cm b e the cost to partition m piles in the Pile-Partition pro cedure. We assume that the pivot pile falls in the interval [xm :1 xm] where 0 x 0:5. Note that x is the split of the piles, not of the elements themselves. Let b e the fraction of the elements in X [ Y that are interchanged in the Partition-Cleanup phase, and let A b e the cost p er element for sucha move.
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]
  • 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]
  • 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]
  • 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]
  • 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]
  • 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]
  • 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]