quicksort

Can't quick sort become stable sort?

こ雲淡風輕ζ 提交于 2019-12-31 02:44:11
问题 Approach 1 C.A.R Hoare introduced partitioning logic(shown below), which is taught in school, low = pivot = 0; i = 1; j = high = listSize-1; while (true) { while (a[i] <= a[pivot] && (i < high)) { i = i + 1; } while (a[j] >= a[pivot] && (j > low)) { j = j - 1; } if (i >= j) break; swap(a[i], a[j]) } swap(a[j], a[pivot]); // pivot element is positioned(once) return j; Approach 2 To basically try make it stable sort , Instead j pointing to last index( listSize-1 ), if j points to listSize/2 (i

Median of 3 partitioning

﹥>﹥吖頭↗ 提交于 2019-12-31 02:08:10
问题 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

Inplace Quicksort in Java

[亡魂溺海] 提交于 2019-12-31 00:39:51
问题 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

Why Merge sort is used for objects in Android/Java API?

和自甴很熟 提交于 2019-12-30 09:51:12
问题 In Java Arrays.sort() for primitive type uses quick sort. On the other hand Arrays.sort() for objects uses Merge sort. And, same goes for Collection.sort() which also uses Merge sort. Collections sort uses Arrays sort implementation underneath. So, in simple sense i can say that primitives are sorted using quick sort but objects are sorted using Merge sort. My guess is it has something to do with sorting algorithm it self. There are so many discussion on SO on Quick sort vs Merge sort, like

Quicksort on Single Linked list

橙三吉。 提交于 2019-12-29 09:59:33
问题 I'm trying to write a simply C code for QUICKSORT on single linked list. Program will get a txt file with password and frequency of usage this password. Program should sort the passwords in order. Can some one tell me how to write function void qsort_list because I don't understand how to get 3 parameters that "partiition()" need. #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct list_element{ char passwort[100]; int haufigkeit; struct list_element *next; } list

In the List<T>.Sort() method, is an item ever compared to itself?

蹲街弑〆低调 提交于 2019-12-29 08:39:21
问题 If I pass in a custom IComparer to an instance of a List's Sort() method, will the comparer's Compare(x,y) method ever be called with the same item? ie. Is it possible that Compare(x,x) may be called. Edit: More interested in the case where items of the list are distinct. 回答1: I wrote a test program to try it out. It looks like it actually does Compare() the same element to itself (at least Compare() is called for the same item twice). In this program, Compare() is called with arguments (2, 2

Building quicksort with php

╄→гoц情女王★ 提交于 2019-12-29 07:42:40
问题 I recently read about quicksort and was wondering whether it would be smart to build my own function to sort things with quicksort or if it would be inefficent. What do you think is the built in sort function better than a selfbuilt quicksort function? 回答1: From http://php.net/sort Note: Like most PHP sorting functions, sort() uses an implementation of » Quicksort. The core PHP functions will be implemented in c, rather than PHP, so they should generally be significantly faster than anything

Building quicksort with php

别等时光非礼了梦想. 提交于 2019-12-29 07:41:12
问题 I recently read about quicksort and was wondering whether it would be smart to build my own function to sort things with quicksort or if it would be inefficent. What do you think is the built in sort function better than a selfbuilt quicksort function? 回答1: From http://php.net/sort Note: Like most PHP sorting functions, sort() uses an implementation of » Quicksort. The core PHP functions will be implemented in c, rather than PHP, so they should generally be significantly faster than anything

Why does QuickSort use O(log(n)) extra space?

女生的网名这么多〃 提交于 2019-12-29 03:53:26
问题 I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)? private static void quickSort (int[] array, int left, int right) { int index = partition(array, left, right); //Sort left half

Why does QuickSort use O(log(n)) extra space?

为君一笑 提交于 2019-12-29 03:53:02
问题 I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)? private static void quickSort (int[] array, int left, int right) { int index = partition(array, left, right); //Sort left half