quicksort

c++ quick sort running time

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 4...

randomized quicksort: probability of two elements comparison?

I am reading "Probability and Computing" by M.Mitzenmacher and E.Upfal. I am having problems understanding how the probability of comparison of two elements is calculated. Input: the sorted list (y1,y2,...,yN) of numbers. We are looking for pivot element (randomly). Question: what is probability that two elements yi and yj (j>i) will be...

What is the difference between quicksort and tuned quicksort?

What is the fundamental difference between quicksort and tuned quicksort? What is the improvement given to quicksort? How does Java decide to use this instead of merge sort? ...

What Sorting Algorithm Is Used By LINQ "OrderBy"?

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 ...

Recursive QuickSort suffering a StackOverflowException -- Need fresh eyes

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 a...

Why does this Quicksort work?

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 pseudoco...

Quicksort / vector / partition issue

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 (){} ...

Quicksort 3 way partition

I want to implement the quicksort 3 way partition. Here is the 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 (;;) { w...

How to change quicksort to output elements in descending order?

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[...

Can't get any speedup from parallelizing Quicksort using Pthreads

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 delega...

about Quick Sort

Hi I have written this code but it will print these stack traces in the console please help me thanks! (Aslo "p" and "q" are the first and last index of our array ,respectively) public class JavaQuickSort { public static void QuickSort(int A[], int p, int q) { int i, last = 0; Random rand = new Random(); if (q < 1) { ...

3-way quicksort, question

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...

Question about my sorting algorithm in C++

i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int n){ int i; int j; if (n<=1) return; for (i=1;i<n;i++) j=0; if (a[i]<a[0]) swap(++j,i,a); swap(0,j,a); qsort5(a,j); qsort(a+j+1,n-j-1); } int main() { return 0; } void swap(int i,in...

Sorting an Array of Arrays in PHP --> Need a Good Algorithm

I have an array of 15000 elements each of which is an array of 4 elements. I want to sort by the second element of the 4. Originally I made the original array's keys the second element and then k-sorted but unfortunately, some of the second elements are duplicates and since one key can't refer to multiple elements i lost some elements in...

Red Black Tree for Sorting

The worst-case running time of insertion on a red-black tree is O(lg n) and if I perform a in-order walk on the tree, I essentially visit each node, so the total worst-case runtime to print the sorted collection would be O(n lg n) I am curious, why are red-black trees not preferred for sorting over quick sort (whose average-case running...

Quick Sort with random pivot in Java

I've been assigned to implement a quick sort with a random pivot point (because it's supposedly the most efficient/safest way), yet I've been slaving over a bogosort. Can anyone direct me on how to do it? And can someone help me look at my bogosort to see if I can save it anyway? public static void Quick(int[] target, int lo, int hi) ...

Writing a Quicksort with one loop

My brother wants me to optimize my code by only having one loop. I don't see how a Quicksort can only have one loop and work. (He told me to remove the inner loop) public class QuickSort { public static void Quick(int[] target, int lo, int hi) { if (lo >= hi) { return; } Random numberGenerator = new Random(); ...

Java: Parallelizing quick sort via multi-threading

I am experimenting with parallelizing algorithms in Java. I began with merge sort, and posted my attempt in this question. My revised attempt is in the code below, where I now try to parallelize quick sort. Are there any rookie mistakes in my multi-threaded implementation or approach to this problem? If not, shouldn't I expect more tha...

QuickSort partition algorithm

Hi, I am trying to program the quicksort algorithm from Cormen Algorithm Textbook. Below is my code. class Quicksort { public void qSort(int[] a, int p, int r) { if(p<r) { int q = Partition(a, p,r); qSort(a, p, q-1); qSort(a, q+1, r); } } private int Partiti...

Quicksort implementation in C?

I really like the qsort function in C. It's so easy to use and allows me to procrastinate learning C++ template types. I have a few questions about this: Is the algorithm used always a quicksort or is it compiler-implementation-dependent? Would you recommend using this function or is there a real benefit to templates? Are there any thi...