quicksort

Optimal Quicksort for Single Linked List

牧云@^-^@ 提交于 2019-11-27 04:32:13
问题 I am working on implementing a quicksort function to sort singly linked lists. What algorithm would I have to use to accomplish this ? For a linked list it would take worst case O(N) for each comparison, instead of the usual O(1) for arrays. So what would the worst case complexity be ? To sum up, what modifications do I need to make to the quicksort algorithm to have an optimal sorting algorithm and what would be the worst case complexity of the algorithm ? Thanks! I have an implementation

Python faster than compiled Haskell?

半城伤御伤魂 提交于 2019-11-27 03:19:58
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 <- readFile "data" let un = lines file let f = map (\x -> read x::Int ) un let done = quicksort f writeFile

Quicksort vs heapsort

谁说胖子不能爱 提交于 2019-11-27 02:39:43
Both quicksort and heapsort do in-place sorting. Which is better? What are the applications and cases in which either is preferred? DVK This paper has some analysis. Also, from Wikipedia: The most direct competitor of quicksort is heapsort. Heapsort is typically somewhat slower than quicksort, but the worst-case running time is always Θ(nlogn). Quicksort is usually faster, though there remains the chance of worst case performance except in the introsort variant, which switches to heapsort when a bad case is detected. If it is known in advance that heapsort is going to be necessary, using it

Quicksort: Iterative or Recursive

不想你离开。 提交于 2019-11-27 01:21:34
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, Which method is preferred in terms of time complexity (memory is not a concern)? Which one is fast

Quicksort superiority over Heap Sort

时光怂恿深爱的人放手 提交于 2019-11-27 00:27:30
Heap Sort has a worst case complexity of O(nlogn) while Quicksort has O(n^2) . But emperical evidences say quicksort is superior. Why is that? One of the major factors is that quicksort has better locality of reference -- the next thing to be accessed is usually close in memory to the thing you just looked at. By contrast, heapsort jumps around significantly more. Since things that are close together will likely be cached together, quicksort tends to be faster. However, quicksort's worst-case performance is significantly worse than heapsort's is. Because some critical applications will require

Intuitive explanation for why QuickSort is n log n?

陌路散爱 提交于 2019-11-26 23:56:44
问题 Is anybody able to give a 'plain english' intuitive, yet formal, explanation of what makes QuickSort n log n? From my understanding it has to make a pass over n items, and it does this log n times...Im not sure how to put it into words why it does this log n times. 回答1: Each partitioning operation takes O(n) operations (one pass on the array). In average, each partitioning divides the array to two parts (which sums up to log n operations). In total we have O(n * log n) operations. I.e. in

C++ template won't accept iterators

只愿长相守 提交于 2019-11-26 23:39:36
问题 I'm re-learning C++, and have started by trying what should be a simple algorithm: QuickSort. My function has this signature: template <class T> void QSort(typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end) And it is called in my main function: int main() { std::vector<int> unsort({56,32,11,45,67,81,12,5}); std::vector<int>::iterator b=unsort.begin(); std::vector<int>::iterator e=unsort.end(); QSort(b, e); return 0; } And gives this error: C:\Users\Deus\Projects

Why does List<T>.Sort method reorder equal IComparable<T> elements?

。_饼干妹妹 提交于 2019-11-26 23:13:17
问题 I have a problem with how the List Sort method deals with sorting. Given the following element: class Element : IComparable<Element> { public int Priority { get; set; } public string Description { get; set; } public int CompareTo(Element other) { return Priority.CompareTo(other.Priority); } } If I try to sort it this way: List<Element> elements = new List<Element>() { new Element() { Priority = 1, Description = "First" }, new Element() { Priority = 1, Description = "Second" }, new Element() {

Stackoverflow with Quicksort Java implementation

旧巷老猫 提交于 2019-11-26 22:04:20
问题 Having some problems implementing quicksort in java. I get a stackoverflow error when I run this program and I'm not exactly sure why. If anyone can point out the error, it would be great. si is the starting index. ei is the ending index. public static void qsort(int[] a, int si, int ei){ //base case if(ei<=si || si>=ei){} else{ int pivot = a[si]; int length = ei - si + 1; int i = si+1; int tmp; //partition array for(int j = si+1; j<length; j++){ if(pivot > a[j]){ tmp = a[j]; a[j] = a[i]; a[i

Quicksort and tail recursive optimization

萝らか妹 提交于 2019-11-26 21:16:32
问题 In Introduction to Algorithms p169 it talks about using tail recursion for Quicksort . The original Quicksort algorithm earlier in the chapter is (in pseudo-code) Quicksort(A, p, r) { if (p < r) { q: <- Partition(A, p, r) Quicksort(A, p, q) Quicksort(A, q+1, r) } } The optimized version using tail recursion is as follows Quicksort(A, p, r) { while (p < r) { q: <- Partition(A, p, r) Quicksort(A, p, q) p: <- q+1 } } Where Partition sorts the array according to a pivot. The difference is that