quicksort

In-Place Quicksort in matlab

风格不统一 提交于 2019-11-28 14:00:00
I wrote a small quicksort implementation in matlab to sort some custom data. Because I am sorting a cell-array and I need the indexes of the sort-order and do not want to restructure the cell-array itself I need my own implementation (maybe there is one available that works, but I did not find it). My current implementation works by partitioning into a left and right array and then passing these arrays to the recursive call. Because I do not know the size of left and and right I just grow them inside a loop which I know is horribly slow in matlab. I know you can do an in place quicksort, but I

Quicksort Pivot

非 Y 不嫁゛ 提交于 2019-11-28 12:12:48
Sort the following array a using quicksort, [6, 11, 4, 9, 8, 2, 5, 8, 13, 7] The pivot should be chosen as the arithmetic mean of the first and the last element, i.e., (a[0] + a[size - 1]) / 2 (rounded down) . Show all important steps such as partitioning and the recursive calls to the algorithm. I understand how to sort the array using quicksort, however I'm not sure how to calculate the pivot. Is the pivot calculated by 6 + 7 = 13 then 13 / 2 = 6.5 (rounded down is 6 ) so the pivot is 2 (i.e. the 6th element)? I know the elements less than pivot appear on the left hand side, and elements

quicksort stack size

为君一笑 提交于 2019-11-28 11:16:01
Why do we prefer to sort the smaller partition of a file and push the larger one on stack after partitioning for quicksort(non-recursive implementation)? Doing this reduces the space complexity of quicksort O(log n) for random files. Could someone elaborate it? As you know, at each recursive step, you partition an array. Push the larger part on the stack, continue working on the smaller part. Because the one you carry on working with is the smaller one, it is at most half the size of the one you were working with before. So for each range we push on the stack, we halve the size of the range we

Quicksort infinite loop if there are repeating values

天涯浪子 提交于 2019-11-28 05:16:33
问题 I have a quicksort program that works great until I try to sort an array that has a repeating number. The program gets stuck in an infinite loop. I believe this is happening in the While(lower < upper) block of code. void quickSort(int array[], int size){ if(size < 2) return; int pivot, lower, upper, temp; //Set the indeces for the first and last elements lower = 0; upper = size - 1; //Select pivot element randomly pivot = array[rand() % (size)]; while(lower < upper){ //Lower must be a number

How to optimize quicksort

江枫思渺然 提交于 2019-11-28 05:02:00
I am trying to work out an efficient quicksort algo. It works okay, but takes long time to run when the number of elements are huge, and certain sections of the array are pre-sorted. I was looking up the Wikipedia article on quicksort , and there I found this written: To make sure at most O(log N) space is used, recurse first into the smaller half of the array, and use a tail call to recurse into the other. Use insertion sort, which has a smaller constant factor and is thus faster on small arrays, for invocations on such small arrays (i.e. where the length is less than a threshold t determined

Stability of quicksort partitioning approach

一世执手 提交于 2019-11-28 05:01:44
Does the following Quicksort partitioning algorithm result in a stable sort (i.e. does it maintain the relative position of elements with equal values): partition(A,p,r) { x=A[r]; i=p-1; for j=p to r-1 if(A[j]<=x) i++; exchange(A[i],A[j]) exchang(A[i+1],A[r]); return i+1; } There is one case in which your partitioning algorithm will make a swap that will change the order of equal values. Here's an image that helps demonstrate how your in-place partitioning algorithm works: We march through each value with the j index, and if the value we see is less than the partition value, we append it to

Intuitive explanation for why QuickSort is n log n?

大城市里の小女人 提交于 2019-11-28 03:11:56
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. 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 average log n partitioning operations and each partitioning takes O(n) operations. Complexity A Quicksort

O(N log N) Complexity - Similar to linear?

与世无争的帅哥 提交于 2019-11-28 02:58:48
So I think I'm going to get buried for asking such a trivial question but I'm a little confused about something. I have implemented quicksort in Java and C and I was doing some basic comparissons. The graph came out as two straight lines, with the C being 4ms faster than the Java counterpart over 100,000 random integers. The code for my tests can be found here; android-benchmarks I wasn't sure what an (n log n) line would look like but I didn't think it would be straight. I just wanted to check that this is the expected result and that I shouldn't try to find an error in my code. I stuck the

C++ template won't accept iterators

萝らか妹 提交于 2019-11-28 02:20:33
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\QSort\main.cpp||In function 'int main()':| C:\Users\Deus\Projects\QSort\main.cpp|49|error: no matching

Stackoverflow with Quicksort Java implementation

你离开我真会死。 提交于 2019-11-28 01:59:11
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] = tmp; i++; } } //put pivot in right position a[si] = a[i-1]; a[i-1] = pivot; //call qsort on right