Search Results

Search found 73 results on 3 pages for 'quicksort'.

Page 1/3 | 1 2 3  | Next Page >

  • Why is Quicksort called "Quicksort"?

    - by Darrel Hoffman
    The point of this question is not to debate the merits of this over any other sorting algorithm - certainly there are many other questions that do this. This question is about the name. Why is Quicksort called "Quicksort"? Sure, it's "quick", most of the time, but not always. The possibility of degenerating to O(N^2) is well known. There are various modifications to Quicksort that mitigate this problem, but the ones which bring the worst case down to a guaranteed O(n log n) aren't generally called Quicksort anymore. (e.g. Introsort). I just wonder why of all the well-known sorting algorithms, this is the only one deserving of the name "quick", which describes not how the algorithm works, but how fast it (usually) is. Mergesort is called that because it merges the data. Heapsort is called that because it uses a heap. Introsort gets its name from "Introspective", since it monitors its own performance to decide when to switch from Quicksort to Heapsort. Similarly for all the slower ones - Bubblesort, Insertion sort, Selection sort, etc. They're all named for how they work. The only other exception I can think of is "Bogosort", which is really just a joke that nobody ever actually uses in practice. Why isn't Quicksort called something more descriptive, like "Partition sort" or "Pivot sort", which describe what it actually does? It's not even a case of "got here first". Mergesort was developed 15 years before Quicksort. (1945 and 1960 respectively according to Wikipedia) I guess this is really more of a history question than a programming one. I'm just curious how it got the name - was it just good marketing?

    Read the article

  • Pseudo-quicksort time complexity

    - by Ord
    I know that quicksort has O(n log n) average time complexity. A pseudo-quicksort (which is only a quicksort when you look at it from far enough away, with a suitably high level of abstraction) that is often used to demonstrate the conciseness of functional languages is as follows (given in Haskell): quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = quicksort [y | y<-xs, y<p] ++ [p] ++ quicksort [y | y<-xs, y>=p] Okay, so I know this thing has problems. The biggest problem with this is that it does not sort in place, which is normally a big advantage of quicksort. Even if that didn't matter, it would still take longer than a typical quicksort because it has to do two passes of the list when it partitions it, and it does costly append operations to splice it back together afterwards. Further, the choice of the first element as the pivot is not the best choice. But even considering all of that, isn't the average time complexity of this quicksort the same as the standard quicksort? Namely, O(n log n)? Because the appends and the partition still have linear time complexity, even if they are inefficient.

    Read the article

  • C# functional quicksort is failing

    - by Rubys
    I'm trying to implement quicksort in a functional style using C# using linq, and this code randomly works/doesn't work, and I can't figure out why. Important to mention: When I call this on an array or list, it works fine. But on an unknown-what-it-really-is IEnumerable, it goes insane (loses values or crashes, usually. sometimes works.) The code: public static IEnumerable<T> Quicksort<T>(this IEnumerable<T> source) where T : IComparable<T> { if (!source.Any()) yield break; var pivot = source.First(); var sortedQuery = source.Skip(1).Where(a => a.CompareTo(source.First()) <= 0).Quicksort() .Concat(new[] { pivot }) .Concat(source.Skip(1).Where(a => a.CompareTo(source.First()) > 0).Quicksort()); foreach (T key in sortedQuery) yield return key; } Can you find any faults here that would cause this to fail?

    Read the article

  • Recursive QuickSort suffering a StackOverflowException -- Need fresh eyes

    - by jon
    I am working on a Recursive QuickSort method implementation in a GenericList Class. I will have a second method that accepts a compareDelegate to compare different types, but for development purposes I'm sorting a GenericList<int I am recieving stackoverflow areas in different places depending on the list size. I've been staring at and tracing through this code for hours and probably just need a fresh pair of (more experienced)eyes. Definitely wanting to learn why it is broken, not just how to fix it. public void QuickSort() { int i, j, lowPos, highPos, pivot; GenericList<T> leftList = new GenericList<T>(); GenericList<T> rightList = new GenericList<T>(); GenericList<T> tempList = new GenericList<T>(); lowPos = 1; highPos = this.Count; if (lowPos < highPos) { pivot = (lowPos + highPos) / 2; for (i = 1; i <= highPos; i++) { if (this[i].CompareTo(this[pivot]) <= 0) leftList.Add(this[i]); else rightList.Add(this[i]); } leftList.QuickSort(); rightList.QuickSort(); for(i=1;i<=leftList.Count;i++) tempList.Add(leftList[i]); for(i=1;i<=rightList.Count;i++) tempList.Add(rightList[i]); this.items = tempList.items; this.count = tempList.count; } }

    Read the article

  • Why does this Quicksort work?

    - by IVlad
    I find this Quicksort partitioning approach confusing and wrong, yet it seems to work. I am referring to this pseudocode. Note: they also have a C implementation at the end of the article, but it's very different from their pseudocode, so I don't care about that. I have also written it in C like this, trying to stay true to the pseudocode as much as possible, even if that means doing some weird C stuff: #include <stdio.h> int partition(int a[], int p, int r) { int x = a[p]; int i = p - 1; int j = r + 1; while (1) { do j = j - 1; while (!(a[j] <= x)); do i = i + 1; while (!(a[i] >= x)); if (i < j) { int t = a[i]; a[i] = a[j]; a[j] = t; } else { for (i = 1; i <= a[0]; ++i) printf("%d ", a[i]); printf("- %d\n", j); return j; } } } int main() { int a[100] = //{8, 6,10,13,15,8,3,2,12}; {7, 7, 6, 2, 3, 8, 4, 1}; partition(a, 1, a[0]); return 0; } If you run this, you'll get the following output: 1 6 2 3 4 8 7 - 5 However, this is wrong, isn't it? Clearly a[5] does not have all the values before it lower than it, since a[2] = 6 > a[5] = 4. Not to mention that 7 is supposed to be the pivot (the initial a[p]) and yet its position is both incorrect and lost. The following partition algorithm is taken from wikipedia: int partition2(int a[], int p, int r) { int x = a[r]; int store = p; for (int i = p; i < r; ++i) { if (a[i] <= x) { int t = a[i]; a[i] = a[store]; a[store] = t; ++store; } } int t = a[r]; a[r] = a[store]; a[store] = t; for (int i = 1; i <= a[0]; ++i) printf("%d ", a[i]); printf("- %d\n", store); return store; } And produces this output: 1 6 2 3 8 4 7 - 1 Which is a correct result in my opinion: the pivot (a[r] = a[7]) has reached its final position. However, if I use the initial partitioning function in the following algorithm: void Quicksort(int a[], int p, int r) { if (p < r) { int q = partition(a, p, r); // initial partitioning function Quicksort(a, p, q); Quicksort(a, q + 1, r); // I'm pretty sure q + r was a typo, it doesn't work with q + r. } } ... it seems to be a correct sorting algorithm. I tested it out on a lot of random inputs, including all 0-1 arrays of length 20. I have also tried using this partition function for a selection algorithm, in which it failed to produce correct results. It seems to work and it's even very fast as part of the quicksort algorithm however. So my questions are: Can anyone post an example on which the algorithm DOESN'T work? If not, why does it work, since the partitioning part seems to be wrong? Is this another partitioning approach that I don't know about?

    Read the article

  • Quicksort / vector / partition issue

    - by xxx
    Hi, I have an issue with the following code : class quicksort { private: void _sort(double_it begin, double_it end) { if ( begin == end ) { return ; } double_it it = partition(begin, end, bind2nd(less<double>(), *begin)) ; iter_swap(begin, it-1); _sort(begin, it-1); _sort(it, end); } public: quicksort (){} void operator()(vector<double> & data) { double_it begin = data.begin(); double_it end = data.end() ; _sort(begin, end); } }; However, this won't work for too large a number of elements (it works with 10 000 elements, but not with 100 000). Example code : int main() { vector<double>v ; for(int i = n-1; i >= 0 ; --i) v.push_back(rand()); quicksort f; f(v); return 0; } Doesn't the STL partition function works for such sizes ? Or am I missing something ? Many thanks for your help.

    Read the article

  • 3-way quicksort, question

    - by peiska
    I am trying to understand the 3-way radix Quicksort, and i dont understand why the the CUTOFF variable there? and the insertion method? public class Quick3string { private static final int CUTOFF = 15; // cutoff to insertion sort // sort the array a[] of strings public static void sort(String[] a) { // StdRandom.shuffle(a); sort(a, 0, a.length-1, 0); assert isSorted(a); } // return the dth character of s, -1 if d = length of s private static int charAt(String s, int d) { assert d >= 0 && d <= s.length(); if (d == s.length()) return -1; return s.charAt(d); } // 3-way string quicksort a[lo..hi] starting at dth character private static void sort(String[] a, int lo, int hi, int d) { // cutoff to insertion sort for small subarrays if (hi <= lo + CUTOFF) { insertion(a, lo, hi, d); return; } int lt = lo, gt = hi; int v = charAt(a[lo], d); int i = lo + 1; while (i <= gt) { int t = charAt(a[i], d); if (t < v) exch(a, lt++, i++); else if (t > v) exch(a, i, gt--); else i++; } // a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]. sort(a, lo, lt-1, d); if (v >= 0) sort(a, lt, gt, d+1); sort(a, gt+1, hi, d); } // sort from a[lo] to a[hi], starting at the dth character private static void insertion(String[] a, int lo, int hi, int d) { for (int i = lo; i <= hi; i++) for (int j = i; j > lo && less(a[j], a[j-1], d); j--) exch(a, j, j-1); } // exchange a[i] and a[j] private static void exch(String[] a, int i, int j) { String temp = a[i]; a[i] = a[j]; a[j] = temp; } // is v less than w, starting at character d private static boolean less(String v, String w, int d) { assert v.substring(0, d).equals(w.substring(0, d)); return v.substring(d).compareTo(w.substring(d)) < 0; } // is the array sorted private static boolean isSorted(String[] a) { for (int i = 1; i < a.length; i++) if (a[i].compareTo(a[i-1]) < 0) return false; return true; } public static void main(String[] args) { // read in the strings from standard input String[] a = StdIn.readAll().split("\\s+"); int N = a.length; // sort the strings sort(a); // print the results for (int i = 0; i < N; i++) StdOut.println(a[i]); } } from http://www.cs.princeton.edu/algs4/51radix/Quick3string.java.html

    Read the article

  • question about quicksort 3 way partition

    - by davit-datuashvili
    i want implement quicksort 3 way partition here is code public class quick3{ public static void quicksort3(int a[],int l,int r){ int k; int v=a[r]; if (r<=l) return; int i=l; int j=r; int p=l-1; int q=r; for (;;) { while (a[++i]<v); while (v<a[--j]) if (j==i) break; if (i>=j) break; swap( a,i, j); if (a[i]==v){ p++; swap(a,p,i);} if (v==a[j]){ q--; swap( a,q,j); } } swap(a,i,r); j=i-1; i=i+1; for (k=1;k<=p;k++,j--) swap(a,k,j); for (k=r-1;k>=q;k--,i++) swap(a,k,i); quicksort3(a,l,j); quicksort3(a,i,r); } public static void main(String[]args){ int a[]=new int[]{4,6,5,9,7,8,3}; quicksort3(a,0,a.length-1); for (int i=0;i<a.length;i++){ System.out.println(a[i]); } } public static void swap(int a[],int i,int j){ int t=a[i]; a[i]=a[j]; a[j]=t; } } after change result is 4 8 7 6 3 5 9 any suggestion?please help

    Read the article

  • Can't get any speedup from parallelizing Quicksort using Pthreads

    - by Murat Ayfer
    I'm using Pthreads to create a new tread for each partition after the list is split into the right and left halves (less than and greater than the pivot). I do this recursively until I reach the maximum number of allowed threads. When I use printfs to follow what goes on in the program, I clearly see that each thread is doing its delegated work in parallel. However using a single process is always the fastest. As soon as I try to use more threads, the time it takes to finish almost doubles, and keeps increasing with number of threads. I am allowed to use up to 16 processors on the server I am running it on. The algorithm goes like this: Split array into right and left by comparing the elements to the pivot. Start a new thread for the right and left, and wait until the threads join back. If there are more available threads, they can create more recursively. Each thread waits for its children to join. Everything makes sense to me, and sorting works perfectly well, but more threads makes it slow down immensely. I tried setting a minimum number of elements per partition for a thread to be started (e.g. 50000). I tried an approach where when a thread is done, it allows another thread to be started, which leads to hundreds of threads starting and finishing throughout. I think the overhead was way too much. So I got rid of that, and if a thread was done executing, no new thread was created. I got a little more speedup but still a lot slower than a single process. The code I used is below. http://pastebin.com/UaGsjcq2 Does anybody have any clue as to what I could be doing wrong?

    Read the article

  • randomized quicksort: probability of two elements comparison?

    - by bantu
    I am reading "Probability and Computing" by M.Mitzenmacher, E.Upfal and I have problems understanding how the probability of comparison of two elements is calculated. Input: the list (y1,y2,...,YN) of numbers. We are looking for pivot element. Question: what is probability that two elements yi and yj (ji) will be compared? Answer (from book): yi and yj will be compared if either yi or yj will be selected as pivot in first draw from sequence (yi,yi+1,...,yj-1,yj). So the probablity is: 2/(y-i+1). The problem for me is initial claim: for example, picking up yi in the first draw from the whole list will cause the comparison with yj (and vice-versa) and the probability is 2/n. So, rather the "reverse" claim is true -- none of the (yi+1,...,yj-1) elements can be selected beforeyi or yj, but the "pool" size is not fixed (in first draw it is n for sure, but on the second it is smaller). Could someone please explain this, how the authors come up with such simplified conclusion? Thank you in advance

    Read the article

  • Quicksort causes stackoverflow...

    - by Tony
    I have the following code, (taken from here), but it causes a stackoverflow exception when there's two the same value's in the list to sort. Can someone help me what's causing this? public static IEnumerable<int> QSLinq(IEnumerable<int> _items) { if (_items.Count() <= 1) return _items; var _pivot = _items.First(); var _less = from _item in _items where _item < _pivot select _item; var _same = from _item in _items where _item == _pivot select _item; var _greater = from _item in _items where _item > _pivot select _item; return QSLinq(_less).Concat(QSLinq(_same)).Concat(QSLinq(_greater)); }

    Read the article

  • How to change quicksort to output elements in descending order?

    - by masato-san
    Hi, I wrote a quicksort algorithm however, I would like to make a change somewhere so that this quicksort would output elements in descending order. I searched and found that I can change the comparison operator (<) in partition() to other way around (like below). //This is snippet from partition() function while($array[$l] < $pivot) { $l++; } while($array[$r] > $pivot) { $r--; } But it is not working.. If I quicksort the array below, $array = (3,9,5,7); should be: $array = (9,7,5,3) But actual output is: $array = (3,5,7,9) Below is my quicksort which trying to output elements in descending order. How should I make change to sort in descending order? If you need any clarification please let me know. Thanks! $array = (3,9,5,7); $app = new QuicksortDescending(); $app->quicksort($array, 0, count($array)); print_r($array); class QuicksortDescending { public function partitionDesc(&$array, $left, $right) { $l = $left; $r = $right; $pivot = $array[($right+$left)/2]; while($l <= $r) { while($array[$l] > $pivot) { $l++; } while($array[$r] < $pivot) { $r--; } if($l <= $r) {// if L and R haven't cross $this->swap($array, $l, $r); $l ++; $j --; } } return $l; } public function quicksortDesc(&$array, $left, $right) { $index = $this->partition($array, $left, $right); if($left < $index-1) { //if there is more than 1 element to sort on right subarray $this->quicksortDesc($array, $left, $index-1); } if($index < $right) { //if there is more than 1 element to sort on right subarray $this->quicksortDesc($array, $index, $right); } } }

    Read the article

  • question about quicksort

    - by davit-datuashvili
    i have write code of quicksort from programming pearls here is code public class Quick{ public static void quicksort(int x[], int l,int u) { if (l>=u) return ; int t=x[l]; int i=l; int j=u; do { i++; } while (i<=u && x[i]<t); do { j--; if (i>=j) break; } while ( x[j]>t); swap(x,i,j); swap(x, l,j); quicksort(x, l,j-1); quicksort(x, j+1,u); } public static void main(String[]args){ int x[]=new int[]{55,41,59,26,53,58,97,93}; quicksort(x,0,x.length-1); for (int i=0;i<x.length;i++){ System.out.println(x[i]); } } public static void swap(int x[], int i,int j){ int s=x[i]; x[i]=x[j]; x[j]=s; } } but it does not work here is output 59 41 55 26 53 97 58 93 any idea?

    Read the article

  • Why does C qicksort function implementation works much slower (tape comparations, tape swapping) than bobble sort function?

    - by Artur Mustafin
    I'm going to implement a toy tape "mainframe" for a students, showing the quickness of "quicksort" class functions (recursive or not, does not really matters, due to the slow hardware, and well known stack reversal techniques) comparatively to the "bubblesort" function class, so, while I'm clear about the hardware implementation ans controllers, i guessed that quicksort function is much faster that other ones in terms of sequence, order and comparation distance (it is much faster to rewind the tape from the middle than from the very end, because of different speed of rewind). Unfortunately, this is not the true, this simple "bubble" code shows great improvements comparatively to the "quicksort" functions in terms of comparison distances, direction and number of comparisons and writes. So I have 3 questions: Does I have mistaken in my implememtation of quicksort function? Does I have mistaken in my implememtation of bubblesoft function? If not, why the "bubblesort" function is works much faster in (comparison and write operations) than "quicksort" function? I already have a "quicksort" function: void quicksort(float *a, long l, long r, const compare_function& compare) { long i=l, j=r, temp, m=(l+r)/2; if (l == r) return; if (l == r-1) { if (compare(a, l, r)) { swap(a, l, r); } return; } if (l < r-1) { while (1) { i = l; j = r; while (i < m && !compare(a, i, m)) i++; while (m < j && !compare(a, m, j)) j--; if (i >= j) { break; } swap(a, i, j); } if (l < m) quicksort(a, l, m, compare); if (m < r) quicksort(a, m, r, compare); return; } } and the kind of my own implememtation of the "bubblesort" function: void bubblesort(float *a, long l, long r, const compare_function& compare) { long i, j, k; if (l == r) { return; } if (l == r-1) { if (compare(a, l, r)) { swap(a, l, r); } return; } if (l < r-1) { while(l < r) { i = l; j = l; while (i < r) { i++; if (!compare(a, j, i)) { continue; } j = i; } if (l < j) { swap(a, l, j); } l++; i = r; k = r; while(l < i) { i--; if (!compare(a, i, k)) { continue; } k = i; } if (k < r) { swap(a, k, r); } r--; } return; } } I have used this sort functions in a test sample code, like this: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <conio.h> long swap_count; long compare_count; typedef long (*compare_function)(float *, long, long ); typedef void (*sort_function)(float *, long , long , const compare_function& ); void init(float *, long ); void print(float *, long ); void sort(float *, long, const sort_function& ); void swap(float *a, long l, long r); long less(float *a, long l, long r); long greater(float *a, long l, long r); void bubblesort(float *, long , long , const compare_function& ); void quicksort(float *, long , long , const compare_function& ); void main() { int n; printf("n="); scanf("%d",&n); printf("\r\n"); long i; float *a = (float *)malloc(n*n*sizeof(float)); sort(a, n, &bubblesort); print(a, n); sort(a, n, &quicksort); print(a, n); free(a); } long less(float *a, long l, long r) { compare_count++; return *(a+l) < *(a+r) ? 1 : 0; } long greater(float *a, long l, long r) { compare_count++; return *(a+l) > *(a+r) ? 1 : 0; } void swap(float *a, long l, long r) { swap_count++; float temp; temp = *(a+l); *(a+l) = *(a+r); *(a+r) = temp; } float tg(float x) { return tan(x); } float ctg(float x) { return 1.0/tan(x); } void init(float *m,long n) { long i,j; for (i = 0; i < n; i++) { for (j=0; j< n; j++) { m[i + j*n] = tg(0.2*(i+1)) + ctg(0.3*(j+1)); } } } void print(float *m, long n) { long i, j; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { printf(" %5.1f", m[i + j*n]); } printf("\r\n"); } printf("\r\n"); } void sort(float *a, long n, const sort_function& sort) { long i, sort_compare = 0, sort_swap = 0; init(a,n); for(i = 0; i < n*n; i+=n) { if (fmod (i / n, 2) == 0) { compare_count = 0; swap_count = 0; sort(a, i, i+n-1, &less); if (swap_count == 0) { compare_count = 0; sort(a, i, i+n-1, &greater); } sort_compare += compare_count; sort_swap += swap_count; } } printf("compare=%ld\r\n", sort_compare); printf("swap=%ld\r\n", sort_swap); printf("\r\n"); }

    Read the article

  • Why is quicksort better than other sorting algorithms in practice?

    - by Raphael
    This is a repost of a question on cs.SE by Janoma. Full credits and spoils to him or cs.SE. In a standard algorithms course we are taught that quicksort is O(n log n) on average and O(n²) in the worst case. At the same time, other sorting algorithms are studied which are O(n log n) in the worst case (like mergesort and heapsort), and even linear time in the best case (like bubblesort) but with some additional needs of memory. After a quick glance at some more running times it is natural to say that quicksort should not be as efficient as others. Also, consider that students learn in basic programming courses that recursion is not really good in general because it could use too much memory, etc. Therefore (and even though this is not a real argument), this gives the idea that quicksort might not be really good because it is a recursive algorithm. Why, then, does quicksort outperform other sorting algorithms in practice? Does it have to do with the structure of real-world data? Does it have to do with the way memory works in computers? I know that some memories are way faster than others, but I don't know if that's the real reason for this counter-intuitive performance (when compared to theoretical estimates).

    Read the article

  • Erlang code explained

    - by dagda1
    Hi, I am having a bit of trouble getting my head around the following erlang code -module(threesix). -export([quicksort/1]). quicksort(Pivot, Left, Right, []=_Src) -> {Left, Pivot, Right}; quicksort(Pivot, Left, Right, [H|T]=_Src) when H < Pivot -> quicksort(Pivot, [H|Left], Right, T); quicksort(Pivot, Left, Right, [H|T]=_Src) -> quicksort(Pivot, Left, [H|Right], T). quicksort([]) -> []; quicksort([H|T]=_List) -> {Left, Pivot, Right} = quicksort(H, [], [], T), quicksort(Left) ++ [Pivot] ++ quicksort(Right). I am specifically talking about the use of _Src and _List in the parameters. Are these simply for documentation as I cannot see why they are used? Thanks Paul

    Read the article

  • LINQ Quicksort is Unstable Except When Cascading

    - by Mystagogue
    On page 64 of "LINQ To Objects Using C# 4.0" (Tony Magennis) he states that LINQ's quicksort ordering algorithm is unstable... ...although this is simply solved by cascading the result into a ThenBy or ThenByDescending operator. Huh? Why would cascading an unstable sortation into another sortation fix the result? In fact, I'd say that isn't possible. The original order, once passed through an unstable sort, is simply lost. What am I missing here?

    Read the article

  • binary quicksort

    - by davit-datuashvili
    hi i want implement Binary quicksort algorithm from robert sedgewick book it looks like this public class quickb{ public static final int bitsword=32; public static void quicksortB(int a[],int l,int r,int d){ int i=l; int j=r-1; if (r<=l || d>bitsword) return ; while (j!=i) { while (digit(a[i],d)==0 && (i<j)) i++; while (digit(a[j],d)==1 && (j>i)) j++; int t=a[i]; a[i]=a[j]; a[j]=t; } if (digit(a[r-1],d)== 0) j++; quicksortB(a,l,j-1,d+1); quicksortB(a,j,r,d+1); } public static void main(String[]args){ int a[]=new int[]{4,7,3,9,8,2}; quicksortB(a,0,a.length-1,0); for (int i=0;i<a.length;i++){ System.out.println(a[i]); } } public static int digit(int m,int d){ return (m>>d)&1; } } but it show me error: java.lang.ArrayIndexOutOfBoundsException: 6 at quickb.quicksortB(quickb.java:13) at quickb.main(quickb.java:32) what is wrong?

    Read the article

  • Quick-sort doesn't work with middle pivot element

    - by Bobby
    I am trying to sort an array of elements using quick-sort algorithm.But I am not sure where am I going wrong.I choose the middle element as pivot every time and then I am checking the conditions.Here is my code below. void quicksort(int *temp,int p,int r) { if(r>p+1) { int mid=(p+r)/2; int piv=temp[mid]; int left=p+1; int right=r; while(left < right) { if(temp[left]<=piv) left++; else swap(&temp[left],&temp[--right]); } swap(&temp[--left],&temp[p]); quicksort(temp,p,left); quicksort(temp,right,r); } }

    Read the article

  • What Sorting Algorithm Is Used By LINQ "OrderBy"?

    - by Mystagogue
    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 actual algorithm LINQ's orderBy is using?

    Read the article

  • c++ quick sort running time

    - by chnet
    I have a question about quick sort algorithm. I implement quick sort algorithm and play it. The elements in initial unsorted array are random numbers chosen from certain range. I find the range of random number effects the running time. For example, the running time for 1, 000, 000 random number chosen from the range (1 - 2000) takes 40 seconds. While it takes 9 seconds if the 1,000,000 number chosen from the range (1 - 10,000). But I do not know how to explain it. In class, we talk about the pivot value can effect the depth of recursion tree. For my implementation, the last value of the array is chosen as pivot value. I do not use randomized scheme to select pivot value. int partition( vector<int> &vec, int p, int r) { int x = vec[r]; int i = (p-1); int j = p; while(1) { if (vec[j] <= x){ i = (i+1); int temp = vec[j]; vec[j] = vec[i]; vec[i] = temp; } j=j+1; if (j==r) break; } int temp = vec[i+1]; vec[i+1] = vec[r]; vec[r] = temp; return i+1; } void quicksort ( vector<int> &vec, int p, int r) { if (p<r){ int q = partition(vec, p, r); quicksort(vec, p, q-1); quicksort(vec, q+1, r); } } void random_generator(int num, int * array) { srand((unsigned)time(0)); int random_integer; for(int index=0; index< num; index++){ random_integer = (rand()%10000)+1; *(array+index) = random_integer; } } int main() { int array_size = 1000000; int input_array[array_size]; random_generator(array_size, input_array); vector<int> vec(input_array, input_array+array_size); clock_t t1, t2; t1 = clock(); quicksort(vec, 0, (array_size - 1)); // call quick sort int length = vec.size(); t2 = clock(); float diff = ((float)t2 - (float)t1); cout << diff << endl; cout << diff/CLOCKS_PER_SEC <<endl; }

    Read the article

1 2 3  | Next Page >