quicksort

Quicksort: Iterative or Recursive

拥有回忆 提交于 2019-11-26 12:27:06
问题 I learnt about quick sort and how it can be implemented in both Recursive and Iterative method. In Iterative method: Push the range (0...n) into the stack Partition the given array with a pivot Pop the top element. Push the partitions (index range) onto a stack if the range has more than one element Do the above 3 steps, till the stack is empty And the recursive version is the normal one defined in wiki. I learnt that recursive algorithms are always slower than their iterative counterpart. So

Multithreaded quicksort or mergesort

早过忘川 提交于 2019-11-26 12:22:23
How can I implement a concurrent quicksort or mergesort algorithm for Java? We've had issues on a 16-(virtual)-cores Mac where only one core (!) was working using the default Java sorting algo and it was, well, not good to see that very fine machine be completely underused. So we wrote our own (I wrote it) and we did indeed gain good speedups (I wrote a multithreaded quicksort and due to its partitioning nature it parallelize very well but I could have written a mergesort too)... But my implementation only scales up to 4 threads, it's proprietary code, and I'd rather use one coming from a

median of three values strategy

五迷三道 提交于 2019-11-26 12:15:16
问题 What is the median of three strategy to select the pivot value in quick sort? I am reading it on the web, but I couldn\'t figure it out what exactly it is? And also how it is better than the randomized quick sort. 回答1: The median of three has you look at the first, middle and last elements of the array, and choose the median of those three elements as the pivot. To get the "full effect" of the median of three, it's also important to sort those three items, not just use the median as the pivot

Why is quicksort better than mergesort?

喜夏-厌秋 提交于 2019-11-26 10:56:51
I was asked this question during an interview. They're both O(nlogn) and yet most people use Quicksort instead of Mergesort. Why is that? Quicksort has O( n 2 ) worst-case runtime and O( n log n ) average case runtime. However, it’s superior to merge sort in many scenarios because many factors influence an algorithm’s runtime, and, when taking them all together, quicksort wins out. In particular, the often-quoted runtime of sorting algorithms refers to the number of comparisons or the number of swaps necessary to perform to sort the data. This is indeed a good measure of performance,

Python faster than compiled Haskell?

心已入冬 提交于 2019-11-26 10:28:15
问题 I have a simple script written in both Python and Haskell. It reads a file with 1,000,000 newline separated integers, parses that file into a list of integers, quick sorts it and then writes it to a different file sorted. This file has the same format as the unsorted one. Simple. Here is Haskell: quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter (< p) xs greater = filter (>= p) xs main = do file <-

is it possible to do quicksort of a list with only one passing?

和自甴很熟 提交于 2019-11-26 09:57:33
问题 I am learning haskell and the function definition I see is: quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more) where less = filter (< x) xs equal = filter (== x) xs more = filter (> x) xs Is it possible to write it with only one traversal of the list, instead of 3? 回答1: Although late, here's a version that's supposed to not leak space as much (and seems to run about twice faster than the other 3-way version here): qsort3 xs = go xs [] where go (x:xs) zs = part x xs zs []

Why does Java&#39;s Arrays.sort method use two different sorting algorithms for different types?

删除回忆录丶 提交于 2019-11-26 06:14:02
问题 Java 6\'s Arrays.sort method uses Quicksort for arrays of primitives and merge sort for arrays of objects. I believe that most of time Quicksort is faster than merge sort and costs less memory. My experiments support that, although both algorithms are O(n log(n)). So why are different algorithms used for different types? 回答1: The most likely reason: quicksort is not stable , i.e. equal entries can change their relative position during the sort; among other things, this means that if you sort

What Sorting Algorithm Is Used By LINQ “OrderBy”?

半腔热情 提交于 2019-11-26 05:36:09
问题 Evidently LINQ\'s \"OrderBy\" had originally been specified as unstable, but by the time of Orca it was specified as stable. Not all documentation has been updated accordingly - consider these links: Jon Skeet on OrderBy stability Troy Magennis on OrderBy stability But if LINQ\'s OrderBy is now \"stable,\" then it means it is not using a quicksort (which is inherently unstable) even though some documentation (e.g. Troy\'s book) says it is. So my question is: if not quicksort, then what is the

Quicksort with Python

孤街醉人 提交于 2019-11-26 03:35:08
问题 I am totally new to python and I am trying to implement quicksort in it. Could someone please help me complete my code? I do not know how to concatenate the three arrays and printing them. def sort(array=[12,4,5,6,7,3,1,15]): less = [] equal = [] greater = [] if len(array) > 1: pivot = array[0] for x in array: if x < pivot: less.append(x) if x == pivot: equal.append(x) if x > pivot: greater.append(x) sort(less) sort(pivot) sort(greater) 回答1: def sort(array=[12,4,5,6,7,3,1,15]): """Sort the

Quicksort: Choosing the pivot

安稳与你 提交于 2019-11-26 00:41:45
问题 When implementing Quicksort, one of the things you have to do is to choose a pivot. But when I look at pseudocode like the one below, it is not clear how I should choose the pivot. First element of list? Something else? function quicksort(array) var list less, greater if length(array) ≤ 1 return array select and remove a pivot value pivot from array for each x in array if x ≤ pivot then append x to less else append x to greater return concatenate(quicksort(less), pivot, quicksort(greater))