quicksort

Hoare Partition Correctness in java

删除回忆录丶 提交于 2020-01-06 08:13:45
问题 I am working to implement a Hoare partition into a quicksort. I am trying to completely understand the hoare partition but the book doesnt explain everything. Mainly I am just wondering what the while TRUE part means? The excerpt from the book is below. If I were to put the while part in java what would I use and why? Hoare-Partition(A,p,r) x= A[p] i = p-1 j=r+1 while true repeat j=j-1 until A [j] <= x repeat i = i +1 until A[i] >= x if i < l exchange A[i] with A[j] else return j 回答1: try

How can I implement a stable quicksort algorithm using O(n) additional space?

巧了我就是萌 提交于 2020-01-06 04:27:06
问题 I am allowed to use an extra array to perform a stable quicksort unlike the general quicksort algorithm. I know how to select the pivot at random and partition accordingly but I'm not able to figure out how to make use of the additional array to make it stable. 回答1: The most trivial way that comes to mind is to store the initial indices in the array (1, 2, 3, etc) and swap them around as you swap the data. Then in the comparison, if the two elements are equal, compare their indices too, thus

Quick Sort - Middle Pivot implementation strange behaviour

折月煮酒 提交于 2020-01-05 23:59:53
问题 I am trying to implement quick sort with pivot value as middle element of the vector using various tutorials available online. Even though it's working for some samples there's one where I am not able to get sorted vector. Example - Input {5,3,8,6,1,0,4} but output is {0,3,4,5,1,6,8} QuickSort implementation void quickSortMiddle(vector<int> &a, int left, int right) { if(left >=right) return; int leftI = left; int rightI = right; int pivot = left + (right - left)/2; while(leftI<=rightI) {

Finding the kth smallest item in an array using quicksort => expected running time?

青春壹個敷衍的年華 提交于 2020-01-04 06:35:26
问题 If I am using a modified version of quicksort to find the kth smallest item in an array, why is the expected running time O(n) (as stated by Programming Pearls book)? The algorithm I'm using does the following: 1) Runs quick sort on the array 2) If k is > the correct location of pivot, then run quicksort on the 2nd half. Otherwise run it on the first half. I was under the impression this would take O(n * logn) work. 回答1: This link, may help to understand the proof for randomized quick select,

What is a Deterministic Quicksort?

流过昼夜 提交于 2020-01-02 01:04:18
问题 I have been reading about Quicksort and found that sometimes it' s referred to as "Deterministic Quicksort". Is this an alternate version of the normal Quicksort ? What is the difference between a normal Quicksort and a Deterministic Quicksort ? 回答1: The ordinary ("deterministic") Quicksort can have very poor behaviour on particular datasets (as an example, an implementation that picks the first unsorted element has O(n^2) time complexity on already-sorted data). Randomized Quicksort (which

quicksort algorithm stability

拥有回忆 提交于 2019-12-31 09:29:13
问题 Quicksort is not stable, since it exchanges nonadjacent elements. Please help me understanding this statement. I know how partitioning works, and what stability is. But I cant figure out what makes the above as the reason for this to be not stable? Then I believe the same can be said for merge sort - though it is quoted to be a stable algorithm. 回答1: Consider what happens during the partition for the following array of pairs, where the comparator uses the integer (only). The string is just

Quick sort partition algorithm

拜拜、爱过 提交于 2019-12-31 07:18:07
问题 void partition(int *a, int size) { int pivot = a[0]; int left = 0, right = 0; for(left = 1, right = size-1; left <= right; left++, right--) { if(a[left] >= pivot && a[right] <= pivot){ swap(left, right, a); } } swap(0, right, a); } I wrote this method to partition an array as a preliminary step in order to apply quick sort, I tested it on this sample data: 8 2 5 13 4 19 12 6 3 11 10 7 9 the correct output should be: 6 2 5 7 4 3 8 12 19 11 10 13 9 but the actual output is: 6 2 5 13 4 3 8 12 19

Quicksort Algorithm (Cormen) gives Stackoverflow

孤街浪徒 提交于 2019-12-31 07:06:27
问题 i implemented the Quick Sort Algorithm which given pseudo code in Introduction to Algorithms (Cormen, 3rd Edition) 7.1 When i tried algorithm with small sized arrays, result is true. But when i tried with N=50000 and array is already sorted like this; N = {1, 2, 3, ..., 50000}; It gives StackOverflowError. I think it's happening because the function recurse itself 50000 times. QuickSort(A, 0, 49999) => QuickSort(A, 0, 49998) => QuickSort(A, 0, 49997)... so go on. Can i solve this problem? Or

Quicksort Algorithm (Cormen) gives Stackoverflow

♀尐吖头ヾ 提交于 2019-12-31 07:06:12
问题 i implemented the Quick Sort Algorithm which given pseudo code in Introduction to Algorithms (Cormen, 3rd Edition) 7.1 When i tried algorithm with small sized arrays, result is true. But when i tried with N=50000 and array is already sorted like this; N = {1, 2, 3, ..., 50000}; It gives StackOverflowError. I think it's happening because the function recurse itself 50000 times. QuickSort(A, 0, 49999) => QuickSort(A, 0, 49998) => QuickSort(A, 0, 49997)... so go on. Can i solve this problem? Or

Combine QuickSort and Median selection algorithm

喜欢而已 提交于 2019-12-31 05:11:11
问题 I want to modify QuickSort (in Java) so that every time Partition is called, the median of the proportioned array is used as the pivot. I have a median selection algorithm in Java that returns the kth smallest element, in this case the median. I have tons of quicksort algorithms in java that all work by themselves and sort an array. Unfortunately I can't combine those two in order to achieve the above... Everytime I try it i usually get stackoverflow erros. Can anybody show me code to see how