quicksort

How to count comaprisons and swaps in Quicksort in Java?

早过忘川 提交于 2019-12-11 04:25:43
问题 I have a simple method to sort an array of int with Quicksort. I dont know how to properly count the number of swaps and comparisons, since the algorithm is recursive: public void quicksort(int tablica[], int x, int y) { int i,j,v,temp; i=x; j=y; int swaps=0; int comparisons=0; v=tablica[(x+y) / 2]; while (i<=j) { while (tablica [i] <v) { i++; } while (tablica [j] >v) { j--; } if (i<=j){ temp = tablica [i]; tablica [i]=tablica[j]; tablica [j] = temp; i++; j--; swaps++; } comparisons++; } if

QuickSort Algorithm Number of Comparisons

淺唱寂寞╮ 提交于 2019-12-11 04:14:26
问题 I have been taking a class at Coursera and we had an assignment which was to count the number of comparisons QuickSort does on a 10,000 size array a numbers. #include <stdio.h> #define SIZE 10000 int ComparsionCount = 0; void swap(int a[], int i, int j) { int temp = a[j]; a[j] = a[i]; a[i] = temp; } int partition(int a[], int l, int r){ int p = a[l]; int i = l + 1; int j; for (j = l + 1; j <= r; j++) { if (a[j] < p) { swap(a, j, i); i++; } } swap(a, l, i - 1); return (i - 1); } void add(int i

How can I make my Quick Sort Algorithm sort the arrays in both Ascending and Descending order?

只谈情不闲聊 提交于 2019-12-11 04:13:00
问题 Currently my Quick Sort algorithm sorts the array in Ascending order in case 1 but I would like to make it so that when the user selects option 2 (case 2) then it sorts the array in Descending order. Do I have to create 2 separate algorithms for each case? Or is there a simpler and more efficient way? Appreciate the help. static void Main(string[] args) { Console.WriteLine("Analysis of Seismic Data.\n"); Console.WriteLine("Selection of Arrays:"); //Display all the options. Console.WriteLine(

SIGSEV in custom QuickSort implementation

陌路散爱 提交于 2019-12-11 03:22:01
问题 I slept over the answer to question Quicksort drama and wanted to recode it from scratch, implementing your tip with the call-by-reference var. And again: I cannot find any failure I made again. I compare the code to your program one by one and I cannot find the problem. The following code produces an Exception (External:SIGSEV at address 11602) during compilation/run program quicksort; var iArray : array[0..8] of integer; procedure fillArray(var iArray : array of integer); begin; iArray[0] :

Does the Linux implementation of quicksort “back off” to insertion sort?

旧城冷巷雨未停 提交于 2019-12-11 01:57:56
问题 I was reading in Bentley & McIlroy (1993) that their suggested implementation of Quicksort uses Insertion Sort when the arrays get small enough. I was curious to know whether modern-day kernels use this same maneuver. Does anyone know whether the Linux kernel, for instance, switches from Quicksort to Insertion Sort in this way? 回答1: Assuming you mean the qsort from the C library, here's the qsort() from a somewhat recent glibc, which is the one used in most Linux systems: http://www.cs.umaine

Quicksort with custom filter

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:13:49
问题 I need to make quicksort but with a custom filter. During compilation I get an error on filter (>=x) xs . --sort with two filters quicksort (x:xs) = (quicksort lesser) ++[x] ++ (quicksort greater) where lesser = filter (<x) xs greater = filter (>=x) xs --sort with custom filter csort f (x:xs) = (csort f lesser) ++ [x] ++ (csort f greater) where lesser = [e | e <- xs, f x e] greater = [e | e <- xs, not $ f x e] What is wrong? 回答1: There are two problems I think might be troubling you. First of

quicksort special case - seems to be a faulty algorithm from K&R

浪尽此生 提交于 2019-12-10 21:26:52
问题 I have a problem understanding quicksort algorithm (the simplified version without pointers) from K&R. There is already a thorough explanation provided by Dave Gamble here explanation. However I noticed that by starting with a slightly changed string we can obtain no swaps during many loops of the for loop. Firstly the code: void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two

Java: Racing Arrays.sort

别等时光非礼了梦想. 提交于 2019-12-10 20:09:34
问题 I created some improvement to QuickSort and decide to test it against Java Arrays.sort() . The results are fascinating: On Java 6: My Time / System Time = 74 / 83 = 0.891566265060241 My Time / System Time = 75 / 79 = 0.9493670886075949 My Time / System Time = 75 / 84 = 0.8928571428571429 On Java 7: My Time / System Time = 115 / 70 = 1.6428571428571428 My Time / System Time = 101 / 76 = 1.3289473684210527 My Time / System Time = 102 / 61 = 1.6721311475409837 As you can see my algorithm

How is stable_partition an adaptive algorithm?

大兔子大兔子 提交于 2019-12-10 19:37:49
问题 stable_partition is a function template present in algorithm header file of c++ STL. I read that it is an adaptive algorithm and its time complexity is O(n*logn) or O(n) depending on some factors.Can someone explain me what are those factors and how the time complexity depends on those factors. Thank You ! 回答1: It depends on how much memory is available. If you have enough memory, one way is to simply create a big enough buffer, insert the respective elements from the front and back and put

Quicksort implementation in Python

泄露秘密 提交于 2019-12-10 16:35:49
问题 I'm trying to implement quicksort in python. However, my code doesn't properly sort (not quite). For example, on the input array [5,3,4,2,7,6,1], my code outputs [1,2,3,5,4,6,7]. So, the end result interposes the 4 and 5. I admit I am a bit rusty on python as I've been studying ML (and was fairly new to python before that). I'm aware of other python implementations of quicksort, and other similar questions on Stack Overflow about python and quicksort, but I am trying to understand what is