quicksort

Python quicksort - List comprehension vs Recursion (partition routine)

我怕爱的太早我们不能终老 提交于 2019-12-19 02:32:44
问题 I watched the talk Three Beautiful Quicksorts and was messing around with quicksort. My implementation in python was very similar to c (select pivot, partition around it and recursing over smaller and larger partitions). Which I thought wasn't pythonic . So this is the implementation using list comprehension in python. def qsort(list): if list == []: return [] pivot = list[0] l = qsort([x for x in list[1:] if x < pivot]) u = qsort([x for x in list[1:] if x >= pivot]) return l + [pivot] + u

C OpenMP parallel quickSort

时光总嘲笑我的痴心妄想 提交于 2019-12-18 13:22:18
问题 Once again I'm stuck when using openMP in C++. This time I'm trying to implement a parallel quicksort. Code: #include <iostream> #include <vector> #include <stack> #include <utility> #include <omp.h> #include <stdio.h> #define SWITCH_LIMIT 1000 using namespace std; template <typename T> void insertionSort(std::vector<T> &v, int q, int r) { int key, i; for(int j = q + 1; j <= r; ++j) { key = v[j]; i = j - 1; while( i >= q && v[i] > key ) { v[i+1] = v[i]; --i; } v[i+1] = key; } } stack<pair<int

Sorting an array using multiple sort criteria (QuickSort)

别来无恙 提交于 2019-12-18 08:50:54
问题 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

How to sort an array in a single loop?

白昼怎懂夜的黑 提交于 2019-12-18 06:09:34
问题 So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insertion sort is O(n) for Best case but is O(n^2) as worst case which again requires 2 loops. Is there a way to sort an array in a single loop? 回答1: Here, a single-loop Bubble Sort in Python: def bubbly_sortish(data): for _ in xrange(len(data)**2): i, j = _/len(data), _%len(data) if i<j and data[i] > data[j]: data[i], data[j] =

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

醉酒当歌 提交于 2019-12-18 02:50:52
问题 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 +

How the usort() sorting algorithm works?

女生的网名这么多〃 提交于 2019-12-17 10:02:03
问题 I have an usort() example and I added some echo statements to see how the code works: <?php function list_cmp($a, $b) { global $order; echo "\$a=$a, \$b=$b </br>"; foreach ($order as $key => $value) { echo "\$value=$value </br>"; if ($a == $value) { echo "\$a=\$value, returing 0. </br>"; return 0; } if ($b == $value) { echo "\$b=\$value, returing 1. </br>"; return 1; } } } $order[0] = 1; $order[1] = 3; $order[2] = 4; $order[3] = 2; $array[0] = 2; $array[1] = 1; $array[2] = 3; $array[3] = 4;

QuickSort and Hoare Partition

梦想与她 提交于 2019-12-17 09:43:31
问题 I have a hard time translating QuickSort with Hoare partitioning into C code, and can't find out why. The code I'm using is shown below: void QuickSort(int a[],int start,int end) { int q=HoarePartition(a,start,end); if (end<=start) return; QuickSort(a,q+1,end); QuickSort(a,start,q); } int HoarePartition (int a[],int p, int r) { int x=a[p],i=p-1,j=r; while (1) { do j--; while (a[j] > x); do i++; while (a[i] < x); if (i < j) swap(&a[i],&a[j]); else return j; } } Also, I don't really get why

Quicksort superiority over Heap Sort

南笙酒味 提交于 2019-12-17 06:29:30
问题 Heap Sort has a worst case complexity of O(nlogn) while Quicksort has O(n^2) . But emperical evidences say quicksort is superior. Why is that? 回答1: One of the major factors is that quicksort has better locality of reference -- the next thing to be accessed is usually close in memory to the thing you just looked at. By contrast, heapsort jumps around significantly more. Since things that are close together will likely be cached together, quicksort tends to be faster. However, quicksort's worst

Quicksort vs heapsort

假装没事ソ 提交于 2019-12-17 05:38:22
问题 Both quicksort and heapsort do in-place sorting. Which is better? What are the applications and cases in which either is preferred? 回答1: This paper has some analysis. Also, from Wikipedia: The most direct competitor of quicksort is heapsort. Heapsort is typically somewhat slower than quicksort, but the worst-case running time is always Θ(nlogn). Quicksort is usually faster, though there remains the chance of worst case performance except in the introsort variant, which switches to heapsort

Quicksort vs heapsort

一曲冷凌霜 提交于 2019-12-17 05:37:18
问题 Both quicksort and heapsort do in-place sorting. Which is better? What are the applications and cases in which either is preferred? 回答1: This paper has some analysis. Also, from Wikipedia: The most direct competitor of quicksort is heapsort. Heapsort is typically somewhat slower than quicksort, but the worst-case running time is always Θ(nlogn). Quicksort is usually faster, though there remains the chance of worst case performance except in the introsort variant, which switches to heapsort