quicksort

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

隐身守侯 提交于 2019-12-04 07:11:10
问题 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

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

空扰寡人 提交于 2019-12-04 06:58:29
问题 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; } }

Why bother with comparison sorts?

别来无恙 提交于 2019-12-04 03:11:32
Algorithms like Timsort, Quicksort & Mergesort dominate the " real world " sorting methods. The case for these comparison sorts is quite practical — they've been shown to be the most performant, stable, multipurpose sorting algorithms in a wide variety of environments. However, it seems like nearly everything that we would sort on a computer are countable / partially ordered. Numbers, characters, strings, even functions are amenable to some meaningful non-comparison sorting method. A candidate here is Radix sort. In general it will behave faster than O(n*log(n)), beating the theoretical

Quick Sort with random pivot in Java

∥☆過路亽.° 提交于 2019-12-03 20:54:34
问题 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) { if(hi-lo==0){return;} Random numberGenerator = new Random(); int pivot = (numberGenerator.nextInt(hi-lo)+lo); int high; for(high=hi; high>pivot ; high--){ if

Java: Parallelizing quick sort via multi-threading

随声附和 提交于 2019-12-03 19:40:40
问题 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 than a 32% speed increase between a sequential and a parallelized algorithm on a duel-core (see timings at bottom)? Here is the multithreading algorithm: public

Quicksort optimizations

空扰寡人 提交于 2019-12-03 17:16:45
I'm learning sorting algorithms and as next step, I'm trying to get my implementation perform close to the std::sort() . I'm pretty far, so far.. :-) I have 3 implementations of quicksort: standard quicksort (using temp arrays). quicksort with following optimizations: median3 used to select median tail-recursion quicksort applied only upto partition sizes < 16. For smaller partitions insertion sort is used. insertion sort applied to the whole array at once instead of applying to each partition, left unsorted by quicksort. quicksort with all optimizations listed above + in-place partitioning

Random pivot quicksort in Java [duplicate]

做~自己de王妃 提交于 2019-12-03 14:11:10
Possible Duplicate: Quick Sort with random pivot in Java The below written code of the Quicksort uses the first element of the array as the pivot and then sorts the array. Now I want to randomly pick up the pivot instead of first one and then sort the array and I am stuck please tell me what changes I can make in the below code to get the perfect results. import java.util.*; import javax.swing.JOptionPane; public class Quicksort { public static void main(String[] args) { String arraylength = JOptionPane.showInputDialog("Enter the length of the array."); int a = Integer.parseInt(arraylength);

C++ : Fastest way to sort a list of number and their index

我是研究僧i 提交于 2019-12-03 13:31:42
I have a question that could seem very basic, but it is in a context where "every CPU tick counts" (this is a part of a larger algorithm that will be used on supercomputers). The problem is quite simple : what is the fastest way to sort a list of unsigned long long int numbers and their original indexes ? (At the beginning, the unsigned long long int numbers are in a completely random order.) Example : Before Numbers: 32 91 11 72 Indexes: 0 1 2 3 After Numbers: 11 32 72 91 Indexes: 2 0 3 1 By "fastest way", I mean : what algorithm to use : std::sort, C qsort, or another sorting algorithm

Improving the Quick sort

痞子三分冷 提交于 2019-12-03 12:13:13
问题 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]<

QuickSort Dijkstra 3-Way Partitioning: why the extra swapping?

余生长醉 提交于 2019-12-03 11:40:18
问题 Given the algorithm here, look at the scenario where i is at "X", the following happens: Scenario: i -> "X", "X" > "P" 1. swap("X", "Z"), gt--; // the value at i is now "Z", which is still > "P" 2. swap("Z", "Y"), gt--; // the value at i is now "Y", which is still > "P" 3. swap("Y", "C"), gt--; // Now we finally get a value at i "C" which is < "P" // Now we can swap values at i and lt, and increrement them 4. swap("P", "C"), i++, lt++; Why don't we just decrement gt until gt points to a value