quicksort

Sorting an array using multiple sort criteria (QuickSort)

六眼飞鱼酱① 提交于 2019-11-29 14:49:19
I am trying to find out how (using a quicksort algorithm) to sort an struct array by 2 criterias. For example say I had a struct of: struct employee{ char gender[12]; char name[12]; int id; }; Say my input is: struct employee arr[3]= { {"male","Matt",1234}, {"female","Jessica",2345}, {"male","Josh",1235} }; I am wanting to sort the elements by gender first then the IDs in ascending order. An example would be have all the males printed first with their IDs in order and then all the females with theirs. I am trying to do this without using the qsort function but I havent the slightest idea how

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

守給你的承諾、 提交于 2019-11-29 14:01:10
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. 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). using System; using System.Collections.Generic; static class Program { class MyComparer : Comparer<int> {

Quicksort infinite loop if there are repeating values

只愿长相守 提交于 2019-11-29 11:51:48
I have a quicksort program that works great until I try to sort an array that has a repeating number. The program gets stuck in an infinite loop. I believe this is happening in the While(lower < upper) block of code. void quickSort(int array[], int size){ if(size < 2) return; int pivot, lower, upper, temp; //Set the indeces for the first and last elements lower = 0; upper = size - 1; //Select pivot element randomly pivot = array[rand() % (size)]; while(lower < upper){ //Lower must be a number < than pivot and upper a number >= pivot while(array[lower] < pivot){ lower++; } while(array[upper] >

Quick Sort in Ruby language

拟墨画扇 提交于 2019-11-29 07:18:21
I am trying to implement Quick sort in ruby but stuck in how to call recursively after the first partition of pivot. Please help me to understand on how to proceed and also let me know whether my style of coding is good so far . class QuickSort $array= Array.new() $count=0 def add(val) #adding values to sort i=0 while val != '000'.to_i $array[i]= val.to_i i=i+1 val = gets.to_i end end def firstsort_aka_divide(val1,val2,val3) #first partition $count = $count+1 @pivot = val1 @left = val2 @right =val3 while @left!=@right do # first divide/ partition logic if $array[@right] > $array[@pivot] then

Using red black trees for sorting

不羁岁月 提交于 2019-11-29 07:10:17
问题 The worst-case running time of insertion on a red-black tree is O(lg n) and if I perform a in-order walk on the tree, I essentially visit each node, so the total worst-case runtime to print the sorted collection would be O(n lg n) I am curious, why are red-black trees not preferred for sorting over quick sort (whose average-case running time is O(n lg n) . I see that maybe because red-black trees do not sort in-place, but I am not sure, so maybe someone could help. 回答1: Knowing which sort

quick sort algorithm improvement if more duplicate keys

人盡茶涼 提交于 2019-11-28 23:46:52
I am reading quick sort algorithm in book by Robert Sedwick Algorithms and data structures part 1-4. template <class item> static void quicksort(item [] a, int l, int r) { if(r <= l) return; int i = partition(a,l,r); quicksort(a, l, i-1); quicksort(a, i+1, r); } template <class item> int partition(item a[], int l, int r) { int i = l-1; j = r; item v = a[r]; for(;;) { while( a[++i] < v ); while( v < a[--j] ) if( j == l ) break; if( i >= j) break; // pointer crossing. exch(a[i], a[j]); } exch(a[i], a[r]); return i; } Book has following text on above algorithm. When duplicate keys are present in

Optimal median of medians selection - 3 element blocks vs 5 element blocks?

为君一笑 提交于 2019-11-28 23:42:08
I'm working on a quicksort-variant implementation based on the Select algorithm for choosing a good pivot element. Conventional wisdom seems to be to divide the array into 5-element blocks, take the median of each, and then recursively apply the same blocking approach to the resulting medians to get a "median of medians". What's confusing me is the choice of 5-element blocks rather than 3-element blocks. With 5-element blocks, it seems to me that you perform n/4 = n/5 + n/25 + n/125 + n/625 + ... median-of-5 operations, whereas with 3-element blocks, you perform n/2 = n/3 + n/9 + n/27 + n/81 +

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

左心房为你撑大大i 提交于 2019-11-28 20:11:36
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 if (left < index - 1) quickSort(array, left, index - 1); //Sort right half if (index < right) quickSort

Comparison between timsort and quicksort

☆樱花仙子☆ 提交于 2019-11-28 17:43:55
Why is it that I mostly hear about quicksort being the fastest overall sorting algorithm when timsort (according to wikipedia) seem to perform much better? Google didn't seem to turn up any kind of comparison. Chang TimSort is highly optimization mergesort, it is stable and faster than old mergesort. when comparing with quicksort, it has two advantages: It is unbelievably fast for nearly sorted data sequence (including reverse sorted data); The worst case is still O(N*LOG(N)). To be honest, I don't think #1 is a advantage, but it did impress me. Here are QuickSort's advantages QuickSort is

why is merge sort preferred over quick sort for sorting linked lists

瘦欲@ 提交于 2019-11-28 15:33:12
问题 I read the following in a forum : Merge sort is very efficient for immutable datastructures like linked lists and Quick sort is typically faster than merge sort when the data is stored in memory. However, when the data set is huge and is stored on external devices such as a hard drive, merge sort is the clear winner in terms of speed. It minimizes the expensive reads of the external drive and when operating on linked lists, merge sort only requires a small constant amount of auxiliary storage