quicksort

Median of 3 partitioning

纵然是瞬间 提交于 2019-12-01 20:50:11
I found the following code for finding a pivot for quicksort using median of first, last and middle element: int middle = ( low + high ) / 2; if( a[ middle ].compareTo( a[ low ] ) < 0 ) swapReferences( a, low, middle ); if( a[ high ].compareTo( a[ low ] ) < 0 ) swapReferences( a, low, high ); if( a[ high ].compareTo( a[ middle ] ) < 0 ) swapReferences( a, middle, high ); // Place pivot at position high - 1 swapReferences( a, middle, high - 1 ); Comparable pivot = a[ high - 1 ]; I want to know after finding the median, why is the swap done with index high-1 instead of high? The reason is that

Weird XOR swap behavior while zeroing out data

好久不见. 提交于 2019-12-01 20:15:34
Thanks Doug. Here's the fix: void swap(int& a, int& b) { if (&a == &b) // added this check to ensure the same address is not passed in return; a ^= b; b ^= a; a ^= b; } I am implementing quicksort for fun in C++, and I am using integers for dummy data. I had been using the XOR swapping algorithm to swap two values in place, but I noticed my sort was screwing up. I changed my swapping algorithm and it worked. I added some debugging statements, and found that the XOR swap was doing something weird. I printed the data before and after I swapped it, and this is what it printed: ... swapping -5, -3

Inplace Quicksort in Java

為{幸葍}努か 提交于 2019-12-01 18:48:17
For refreshing some Java I tried to implement a quicksort (inplace) algorithm that can sort integer arrays. Following is the code I've got so far. You can call it by sort(a,0,a.length-1) . This code obviously fails (gets into an infinite loop) if both 'pointers' i,j point each to an array entry that have the same values as the pivot. The pivot element v is always the right most of the current partition (the one with the greatest index). But I just cannot figure out how to avoid that, does anyone see a solution? static void sort(int a[], int left, int right) { if (right > left){ int i=left, j

Quicksort not sorting correctly

坚强是说给别人听的谎言 提交于 2019-12-01 14:55:13
Attempting to learn from doing an implementation of Quicksort, I cannot find out why it's not sorting properly. Using this sequence: 6, 7, 12, 5, 9, 8, 65, 3 It returns this: 3, 5, 7, 8, 9, 65, 12, 6 It seems to sort somewhat, but not all. What have I missed? Here's my code: static void Main(string[] args) { QuickSort qs = new QuickSort(); int[] arr = new int[] { 6, 7, 12, 5, 9, 8, 65, 3 }; foreach (int l in arr) { Console.Write(l + ", "); } int left = 0; int right = arr.Count() - 1; int[] arrr = qs.DoQuickSort(ref arr, left, right); Console.WriteLine("Sorted List: "); foreach (int i in arrr)

How to count comparisons and swaps in quicksort?

柔情痞子 提交于 2019-12-01 12:42:33
问题 public class IntQuickSorter { public static int numOfComps = 0, numOfSwaps = 0; public static void main(String[] args) { // Create an int array with test values. int[] values = { 1, 2, 3, 4, 5, 6 }; //int[] values = { 5, 1, 3, 6, 4, 2 }; //int[] values = { 5, 7, 2, 8, 9, 1 }; System.out.println("\n\nQuick Sort:"); // Display the array's contents. System.out.println("\nOriginal order: "); for (int element : values) System.out.print(element + " "); // Sort the array. quickSort(values); //System

Java: How to sort custom type ArrayList

陌路散爱 提交于 2019-12-01 08:30:16
I have a custom type Position(x,y,z) ,now I create a ArrayList<Position> , i want to sort this array ordered by the value of z, from small to bigger,how can i do that using Collections.sort or is there any other efficient sorting method? When I try to use public class PositionComparator implements Comparator<Position> { @Override public int compare(Position o1, Position o2) { // TODO Auto-generated method stub return o1.height().compareTo(o2.height()); } } get an error Cannot invoke compareTo(double) on the primitive type double try Collections.sort(SortList, new Comparator<Position>(){ public

Got stackoverflowerror when using quickSort, can I increase the stack and the heap?

寵の児 提交于 2019-12-01 07:44:19
问题 Can I increase the stack and the heap in java? I'm using BlueJ. ======== EDIT: Here is the code: // ***** Quick-Sort Method ***** public static void quickSort(int[] data, int first, int n) { int p, n1, n2; if(n > 1) { p = partition(data, first, n); n1 = p - first; n2 = n - n1 - 1; quickSort(data, first, n1); quickSort(data, p+1, n2); } } // ***** PRIVATE HELPER FUNCTIONS ***** public static void quickSort(int[] data) { quickSort(data, 0, data.length); } private static int partition(int[] A,

Quick sort median selection

扶醉桌前 提交于 2019-12-01 07:40:41
问题 As we can choose median of 3 element partitioning to implement quick sort. Likewise can we choose median of 5, 7, or 11 element to implement quick sort? If so, then how?? 回答1: You should look into the Median of Medians algorithm. It is a linear time algorithm with the following recurrence... T(n) ≤ T(n/5) + T(7n/10) + O(n) ... which is O(n). The algorithm details... divide the list into n/5 subsequences of 5 elements each find the median of each list, by brute force. there will be n/5 of

C randomized pivot quicksort (improving the partition function)

走远了吗. 提交于 2019-12-01 05:12:16
问题 I'm a computer science student (just started), I was working on writing from pseudocode a randomized pivot version of Quicksort. I've written and tested it, and it all works perfectly however... The partition part looks a bit too complicated, as it feels I have missed something or overthought it. I can't understand if it's ok or if I made some avoidable mistakes. So long story short: it works, but how to do better? Thanks in advance for all the help void partition(int a[],int start,int end) {

How to reify Prolog's backtracking state to perform the same task as “lazy seq” from Clojure?

为君一笑 提交于 2019-12-01 02:07:49
问题 Here is a quicksort algorithm for numbers written in Clojure. It is basically the quicksort algorithm found in "The Joy of Clojure" , 2nd edition, page 133. I modified it slightly for (hopefully) better readability, because the original felt a bit too compact: (defn qsort-inner [work] (lazy-seq (loop [loopwork work] (let [[ part & partz ] loopwork ] (if-let [[pivot & valuez] (seq part)] (let [ smaller? #(< % pivot) smz (filter smaller? valuez) lgz (remove smaller? valuez) nxxt (list* smz