quicksort

What is the advantage of using tail recursion here?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 02:56:52
I have been reading articles describing how space complexity of quicksort can be reduced by using the tail recursive version but I am not able to understand how this is so. Following are the two versions : QUICKSORT(A, p, r) q = PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r) TAIL-RECURSIVE-QUICKSORT(A, p, r) while p < r q = PARTITION(A, p, r) TAIL-RECURSIVE-QUICKSORT(A, p, q-1) p = q+1 (Source - http://mypathtothe4.blogspot.com/2013/02/lesson-2-variations-on-quicksort-tail.html ) As far as I understand , both of these would cause recursive calls on both the left and right half of

Improving the Quick sort

人走茶凉 提交于 2019-12-03 02:37:28
If possible, how can I improve the following quick sort(performance wise). Any suggestions? void main() { quick(a,0,n-1); } void quick(int a[],int lower,int upper) { int loc; if(lower<upper) { loc=partition(a,lower,upper); quick(a,lower,loc-1); quick(a,loc+1,upper); } } /* Return type: int Parameters passed: Unsorted array and its lower and upper bounds */ int partition(int a[],int lower,int upper) { int pivot,i,j,temp; pivot=a[lower]; i=lower+1; j=upper; while(i<j) { while((i<upper)&&(a[i]<=pivot)) i++; while((a[j]>pivot)) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }//end while if

Apply Quick sort algorithm to Doubly linked list by changing nodes

北城余情 提交于 2019-12-03 00:49:52
问题 I need to sort the doubly linked list by using quicksort algorithm. Used recursion for sorting. And my partitioning function is same as the one we used in arrays. But I faced a hard time with tracking the current head node and tail node in each list. public void sort() { Node la = getLast(head); last = la; quickSort(head, last); } public void quickSort(Node newhead, Node newlast) { if(newhead == null || newlast == null) { return; } if(newhead == newlast) { return; } Node parti = partition

Quick sort programmed in C

左心房为你撑大大i 提交于 2019-12-02 20:57:43
问题 I am reading ANSI C by K&R. I came across the qsort program. I want a little help. Suppose I have 9 elements with index 0->8. Please read the comments to see if I am understanding it correct or not. Thanks a lot for you efforts void qsort(int v[] , int left, int right) { int i, j, last; void swap(int v[], int i, int j); if(left >= right) /*if the array has only one element return it*/ return; swap(v,left, (left+right)/2); /* now, left=(left+right)/2= 0+8/2= 4 we have 4 as left*/ last= left; /

Implementing quicksort algorithm

回眸只為那壹抹淺笑 提交于 2019-12-02 20:25:50
I found quicksort algorithm from this book This is the algorithm QUICKSORT (A, p, r) if p < r q = PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r) PARTITION(A, p, r) x=A[r] i=p-1 for j = p to r - 1 if A <= x i = i + 1 exchange A[i] with A[j] exchange A[i+1] with A[r] return i + 1 And I made this c# code: private void quicksort(int[] input, int low, int high) { int pivot_loc = 0; if (low < high) pivot_loc = partition(input, low, high); quicksort(input, low, pivot_loc - 1); quicksort(input, pivot_loc + 1, high); } private int partition(int[] input, int low, int high) { int pivot =

Is it possible to quicksort objects based on their keys in an array, using JavaScript?

﹥>﹥吖頭↗ 提交于 2019-12-02 18:59:24
问题 To clarify a bit on the question, I have an array and within the array are a bunch of objects. Can I rearrange the objects in the array based on the key values of each object? When I'm attempting to do so, it keeps telling me that the variable (in this case, students) is undefined. When I use the built-in sort function, it works perfectly. However, this is a school assignment and I HAVE to show the breakdown of the quicksort function. Here is the code I am using: function swap(student,

Simple QuickSort Algorithm giving Stack Overflow Error?

瘦欲@ 提交于 2019-12-02 18:46:30
问题 My friend has a small problem and I'm at the end of my knowledge. He wrote a Simple (he got it in school) QuickSort Algorithm And it produces a StackOverflow Error. I know it means it calls itself recursive too many times somewhere, but I can't get the logical Error - please help me! Here is the code (I'm leaving out some code as that is only to show it in 2 text areas): int array [] = new int [10]; ... public static void quicksort (int array[],int l,int r){ int i = l; int j = r; int mitte =

change pivot in my quickSort algorithm java

大兔子大兔子 提交于 2019-12-02 18:15:11
问题 I have implemented a working quickSort algorithm using the first element in the array as the pivot, that look like this: public int[] quickSort( int[] a, int start, int end){ int l = start; int r = end; int pivotIndex = start; //<---- first element in the array as pivot! // must be at least two elements if ( end - start >= 1){ // set pivot int pivot = a[pivotIndex]; while ( r > l ){ // scan from the left while ( a[l] <= pivot && l <= end && r > l ){ l++; } while ( a[r] > pivot && r >= start &

Quicksort with 3-way partition

十年热恋 提交于 2019-12-02 17:27:44
What is QuickSort with a 3-way partition? Picture an array: 3, 5, 2, 7, 6, 4, 2, 8, 8, 9, 0 A two partition Quick Sort would pick a value, say 4, and put every element greater than 4 on one side of the array and every element less than 4 on the other side. Like so: 3, 2, 0, 2, 4, | 8, 7, 8, 9, 6, 5 A three partition Quick Sort would pick two values to partition on and split the array up that way. Lets choose 4 and 7: 3, 2, 0, 2, | 4, 6, 5, 7, | 8, 8, 9 It is just a slight variation on the regular quick sort. You continue partitioning each partition until the array is sorted. The runtime is

When should we use Radix sort?

北城余情 提交于 2019-12-02 16:34:07
It seems Radix sort has a very good average case performance, i.e. O(kN) : http://en.wikipedia.org/wiki/Radix_sort but it seems most people still are using Quick Sort, don't they? Quick sort has an average of O(N logN), but it also has a worst case of O(N^2), so even due in most practical cases it wont get to N^2, there is always the risk that the input will be in "bad order" for you. This risk doesn't exist in radix sort. I think this gives a great advantage to radix sort. Radix sort is harder to generalize than most other sorting algorithms. It requires fixed size keys, and some standard way