quicksort

Hoare partition doesn't work?

こ雲淡風輕ζ 提交于 2019-12-02 16:31:48
问题 I have been trying to implement the Hoare partitioning method, but neither I, nor the computer can't seem to understand it as it is written in Cormen and Wikipedia. The algorithm in both sources looks like this: algorithm partition(A, lo, hi) is pivot := A[lo] i := lo - 1 j := hi + 1 loop forever do j := j - 1 while A[j] > pivot do i := i + 1 while A[i] < pivot if i < j then swap A[i] with A[j] else return j For the following array: 9 3 11 55 4 , after partitioning it with the above function,

Sort arrays with pointers using quicksort

怎甘沉沦 提交于 2019-12-02 16:09:25
问题 I'm trying to sort the values in the array "tab" using quicksort, but it isn't working. the main function just set names and salaries for each tab[n] typedef struct employee Employee; struct employee{ char name[81]; float salary; }; Employee *tab[10]; void sort(Employee **tab, int begin, int end){ int p = tab[(begin + end) / 2] , i = end, j = begin; /*p is the pivot*/ do{ while(tab[i]->salary < p && i < end) i++; while(tab[j]->salary > p && j > begin) j--; if(i <= j){ int tmp = tab[i]->salary

How can I implement a quick sort in Delphi without getting Access violation errors for large numbers of records?

会有一股神秘感。 提交于 2019-12-02 14:50:55
Here is my current code: function StudentQuickSort(StudentList:TStudentArray;ArrayLength:integer):TStudentArray; var Pivot:TstudentArray; LesserList:TStudentArray; GreaterList:TstudentArray; ArrayCount:Integer; LesserCount:Integer; GreaterCOunt:integer; procedure ConcatArrays(const A,B,C: TStudentArray; var D: TStudentArray); var i, nA,nB,nC: integer; begin nA := length(A); nB := length(B); nC := Length(C); SetLength(D,nA+nB+nC); for i := 0 to nA-1 do D[i] := A[i]; for i := 0 to nB-1 do D[i+nA] := B[i]; for i := 0 to nC-1 do D[i+nA+nB] := C[i]; end; begin if Arraylength<=1 then begin Result:=

Apply Quick sort algorithm to Doubly linked list by changing nodes

匆匆过客 提交于 2019-12-02 14:35:39
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(newhead,newlast); if(parti != head) quickSort(newhead, parti.prev); if(parti != last) newlast = acualTail;

What is the purpose of these lines of swap code in quicksort application?

梦想的初衷 提交于 2019-12-02 14:01:28
I am trying to understand an implementation or an application of quicksort to find the kth smallest element Here is the code that I am trying to understand public int quicksort(int a[], int start, int end, int k) { if(start < end) { int pivot = partition(a, start, end); if(pivot == k -1) { return pivot; } else if(pivot > k - 1){ return quicksort(a, start, pivot, k); } else { return quicksort(a, pivot + 1, end, k); } } else if(start == end) { return k==1?start:-1; } else { return -1; } } public int partition(int a[], int start, int end) { int pivot = start; int i = pivot + 1; int j = end; while

Quick sort programmed in C

南楼画角 提交于 2019-12-02 13:31:06
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; /* putting left = last of the first partition group i.e. last=4*/ for(i=left+1; i<=right,i++) /* now

Simple QuickSort Algorithm giving Stack Overflow Error?

安稳与你 提交于 2019-12-02 13:08:40
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 = array [(l+r)/2]; while (i<j) { while (array[i]<mitte) { i++; } // end of if while (mitte<array[i]) { j-

Hoare partition doesn't work?

时间秒杀一切 提交于 2019-12-02 10:48:05
I have been trying to implement the Hoare partitioning method, but neither I, nor the computer can't seem to understand it as it is written in Cormen and Wikipedia. The algorithm in both sources looks like this: algorithm partition(A, lo, hi) is pivot := A[lo] i := lo - 1 j := hi + 1 loop forever do j := j - 1 while A[j] > pivot do i := i + 1 while A[i] < pivot if i < j then swap A[i] with A[j] else return j For the following array: 9 3 11 55 4 , after partitioning it with the above function, it will look like this: 4 3 11 55 9 and the pivot index will be 2 which is completely false. Firstly,

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

痞子三分冷 提交于 2019-12-02 09:45:25
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, firstIndex, secondIndex){ var temp = student[firstIndex]; student[firstIndex] = student[secondIndex]; student

Quicksort Algorithm (Cormen) gives Stackoverflow

家住魔仙堡 提交于 2019-12-02 08:21:01
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 should i use different pivot position? Here is my code; public void sort(int[] arr){ QuickSort(arr, 0,