FAQ Database Discussion Community


If the median of medians algorithm doesn't change the avg-case complexity of quicksort, why use it?

algorithm,sorting,selection,quicksort
Given the hard lower bound on the sorting algorithm's average case complexity Omega(n*lg(n)), when/why would you decide to spend the time implementing this selection algorithm with quicksort rather than just using a random pivot or just the simple (n/2)th position in the array?

HeapSort/IntrospectiveSort on Array Portion Java implementation

java,arrays,sorting,quicksort,heapsort
I am currently studying sorting algorithms and need to implement HeapSort and Introspective Sort. I think I have implemented HeapSort successfully (the code works, tried on millions of random arrays with random sizes, always worked), here's my code: public static <T extends Comparable<? super T>> void hsort(T[] a) { int...

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

java,android,sorting,quicksort,mergesort
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...

Time complexity for modification on quicksort

algorithm,sorting,data-structures,time-complexity,quicksort
Let a function named QUARTERSORT which gets an array and sort it in the following way: If n<100` it uses the regular QUICKSORT Otherwise, we split the array to on A1 = A[1,...,n/4] and A2 = A[(n/4)+1,...,n]. Then, we call QUARTERSORT twice: B1 = QUARTERSORT(A1) and B2 = QUARTERSORT(A2). Finally,...

Seg Fault in c++ quicksort algorithm on array of 500,000 sorted integers

c++,algorithm,sorting,segmentation-fault,quicksort
I am having a problem with my quicksort algorithm in c++. I am running on 3 different input files of 500,000 integers each. The 1st and 3rd run as they should, while the 2nd causing a segfault. The 1st has 500,000 unsorted integers, the 2nd has 500,000 sorted integers, and...

QuickSort stack overflow on big input

java,stack-overflow,quicksort
My QuickSort implementation causes StackOverflow error if I give reverse-sorted array. It is working fine for about 1000 items, but for 10000+ I get StackOverflow error. If I get the error the recursion depth is about 9000. I know my algorithm always choose the latest element of the subarray as...

Quicksort with 2 way partitioning

c#,algorithm,quicksort
I am trying to implement quicksort algorithm by Sadgewick. The code was taken from here http://www.sorting-algorithms.com/static/QuicksortIsOptimal.pdf Here is what was in the book: void quicksort(Item a[], int l, int r) { int i = l-1, j = r; Item v = a[r]; if (r <= l) return; for (;;) {...

Quicksort: wikipedia-implementation not working out

java,algorithm,sorting,quicksort
I just tried to implement the quicksort algorithm from wikipedia (https://en.wikipedia.org/wiki/Quicksort) but something is missing. Here's my code: public static void quicksort(int[] a, int lo, int hi) { if(lo < hi) { int p = partition(a, lo, hi); quicksort(a, lo, p - 1); quicksort(a, p + 1, hi); } }...

how i could fix this wrong in algoritimo quicksort [closed]

java,sorting,quicksort
well, I have been doing a program where I need to sort words, a array String. I am using the Algorithm Quick Sort and it works nice when I sort for the first time. But when I try to sort the self array when already ordered it throws a error...

QuickSort in Java not delivering sorted results

java,quicksort
I tried to implement the code for QuickSort using the algorithm which was mentioned on http://en.wikipedia.org/wiki/Quicksort#Algorithm . But, I am not able to get a sorted output. Rather, I am only getting the same array as the output. Can anybody review it for me and tell me what's wrong with...

Quicksort occasionally not completing?

java,algorithm,sorting,quicksort
I've made a bunch of attempts at a quicksort algorithm, which I just can't seem to make work. This code is the closest I've gotten, except that it about one in five times it doesn't fully sort - it outputs something like 266, 186, 219, 276, 357, 405, 686, 767,...

What is error in partition code?

c,quicksort,partitioning
Below code is working for Array{ 4 5 3 7 2 }, but not working for other test case given on HackerRank Site. What is error in my code? Am I doing any wrong while merging the two array a1[] and a2[] into the ar[] ? https://www.hackerrank.com/challenges/quicksort1 #include <stdio.h> #include...

Quicksort intermediate list printing Python

python,quicksort,in-place
The prompt for this HackerRank problem has me stumped. It is essentially a quicksort implemention but as an exception you are required to print the intermediate (or semi-sorted) "original" array in its entirety each iteration. My working code without printing intermediates. It works as expected. def quicksort(array): if len(array) >...

Stackoverflow exception caused by recursive call of partitionStep in QuickSort

java,recursion,quicksort
I try to implement quicksort algorithm for int[]. I coded the partition step correctly but then I wanted to call partition step method recursively to get the array sorted, unfortunately I pass wrong indexes to the recursive call and I get StackOverflow exception. I based it on: https://www.youtube.com/watch?v=y_G9BkAm6B8 public class...

Correctness of Hoare Partition

algorithm,sorting,data-structures,quicksort,hoare-logic
Hoare partition as given in cormen: Hoare-Partition(A, p, r) x = A[p] i = p - 1 j = r + 1 while true repeat j = j - 1 until A[j] <= x repeat i = i + 1 until A[i] >= x if i < j swap( A[i],...

how to set the pivot in quick sort [closed]

c++,quicksort
I just update my codes it works but there some problems when I run it. I put some "cout" to debug my partiton part and I don't know whay it always set 0 to pivot.(the pivot should be last number that I inputed) Here is my code int partiton(vector<int> &a,...

Using insertion sort to sort only part of array

java,quicksort
I'm writing a quicksort program. Part of the quicksort involves using insertionsort, but it only sorts a certain range of elements, since quicksort handles the rest. I'm trying to emulate a method provided by my textbook that uses public static void insertionSort(int a[], int left, int right) But I am...

How to make sense of or interpret the first call to partition in Quicksort?

java,arrays,algorithm,sorting,quicksort
This covers "a software algorithm" from http://stackoverflow.com/help/on-topic or in this case, the quicksort algorithm to sort a set of numbers. Here is the quicksort algorithm code I am using (From cracking the coding interview 5th edition) static void quickSort(int[] arr, int left, int right) { int index = partition(arr, left,...

Never-ending quicksort

c,infinite-loop,quicksort,gcc4.9
I tried to implement a quicksort on arrays of int64_t like this: void quicksort (int64_t *array,size_t size) { int64_t *split; size_t i=0; size_t j=size-1; if (size>1) { split=({ int64_t p=array[0]; do { for (;array[i]<p;i++); for (;array[j]>p;j--); swap(array[i],array[j]); } while (i<j); swap(array[i],array[j]); swap(array[j],array[size]); &(array[j]); })-1; quicksort(array,j-1); quicksort(split+1,size-j); } return; } Which...

Sorting arrays with quickSort (JAVA)

java,arrays,quicksort
Okay, so I need to sort an array in both ascending and descending order using quick sort. I have code that works properly for the ascending, and properly most of the time for descending, but it will randomly not work well for descending... most often when the smallest number is...

Partition in quick sort logical error

c++,sorting,logic,quicksort
int partition(int A[], int low, int high){ int mid = (low+high)/2; int pivot = A[mid]; while(low <= high) { while(low <= high && A[high] >= pivot) { high--; } while (low <= high && A[low] <= pivot) { low ++; } if(low <= high) { int tmp = A[low]; A[low]...

QuickSort stack overflow for sorted arrays (works for other data sets)

java,algorithm,data-structures,stack-overflow,quicksort
So I tried my best to optimize my Quicksort algorithm to run as efficiently as possible, even for sorted or nearly sorted arrays, using a pivot that is the median of three values, and also using insertion sort for small partition sizes. I have tested my code for large arrays...

Is it possible to quicksort objects based on their keys in an array, using JavaScript?

javascript,arrays,quicksort
To clarify a bit on the question, I have an array and within the array are a bunch of objects. Can I rearrange the objects in the array based on the key values of each object? When I'm attempting to do so, it keeps telling me that the variable (in...

How to get partition of quicksort to produce the correct and expected output?

java,arrays,algorithm,sorting,quicksort
This concerns "a software algorithm" from http://stackoverflow.com/help/on-topic, in this case the quicksort sorting algorithm This is a practice coding question(non competition) from https://www.hackerrank.com/challenges/quicksort1 Basically you're supposed to take in a list, say 4 5 3 7 2 and partition around the first element, in this case 4. The expected output...

Creating quicksort without recursion and stack

java,quicksort
I have a task to write quicksort (on only posivite numbers) algorythm in Java (I can't use any imports but Scanner) but without recursion and without stack. I have two question about it : I do understeand iterative quicksort with stack and recursive version but i cannot imagine how to...

Recursion calls overflows stack

java,arrays,recursion,quicksort
I'm having a problem with the partitioning while trying to implement the Quicksort algorithm. My implementation works good with arrays up to a size of 10,000 but over that I'm getting a StackOverflowError. Note that this only happens when the input arrays are in a/descending order. Random-ordered arrays can be...

My QuickSort and MergeSort are not functioning Properly

java,string,sorting,arraylist,quicksort
My MergeSort and QuickSort call Insertion Sort when the segment is <= 15 indices in size. Insertion Sort works fine, Merge works fine, and Partition works fine, but my MergeSort and QuickSort do NOT work properly... They do not seem to sort at all... Please help: Here is my Insertion...

How to implement quicksort on a double-linked list of pointers?

delphi,pointers,quicksort,delphi-2009
I have the code for quicksorting an array of pointers (if helps anyone) but how I do that for a doble linked list of pointers ? procedure TSuperList.Sort; begin if Assigned(FOnCompare) and (Length(Items)>1) then QuickSort(0,High(Items)); end; procedure TSuperList.QuickSort(L,R:Integer); var I,J: Integer; P,T: Pointer; begin repeat I:=L; J:=R; P:=Items[(L+R) shr 1];...

Hoare's partition not working correctly (quicksort)

c++,algorithm,quicksort
So after following the quicksort and hoares partition algorithm from Cormen, this is the code that I was able to produce. The array comes out partly sorted with uninitialized elements/garbage elements and I can't for the life of me figure out why... I thought I followed the algorithm exactly as...

QuickSort's estimation of recursion depth

algorithm,recursion,quicksort
Being the recursion depth the maximum number of successive recursive calls before QuickSort hits it´s base case, and noting that it (recursion depth) is a random variable, since it depends on the chosen pivot. What I want is to estimate the minimum-possible and maximum-possible recursion depth of QuickSort. The following...

quick sort on multidimensional array

java,recursion,multidimensional-array,quicksort
Edit: I want this method to sort in ascending order based on any column the user want (each data in the same respective row is 'attached' to each other). There are 4 column in the table. if the user want to sort in based on first column then he should...

QuickSort Code (Python)

python,quicksort
this is my first post on StackExchange, and I'm trying to figure out what's wrong with my code for a simple QuickSort program. I'm fairly sure that some integer simply needs to be adjusted by +-1 or something, so I'd like to keep the format. The code is as follows:...

QuickSort with Double Linked List

java,quicksort,doubly-linked-list
I am having trouble with my swap method in the Quick Sort program. I'm implementing it from a QuickSort method that sorts arrays. Here I take in a file with an integer on each line, it puts the number in a doubly linked list and then sorts the list and...

Index Out of Bounds Exception when writing QuickSort Algorithm

java,quicksort
I'm currently writing a modified version of a quick sort algorithm and I'm getting an index out of bounds exception even though my ArrayList input has elements in it. Hopefully someone can tell me where I'm going wrong... Here is the error code: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4,...

Understanding the following Quicksort Implementation

java,quicksort
I have the following two methods : public static void QuickAlgo(int[] a, int left, int right) { int index = partition(a, left, right); if (left < index - 1) { QuickAlgo(a, left, index - 1); } if (index < right) { QuickAlgo(a, index, right); } } static int partition(int[] a,...

Recursive QuickSort multiple bugs

java,recursion,quicksort
I have been trying to fix this recursive quick sort program for about three days now and I believe it has bugs in it because it sorts smaller size arrays but wrongly sorts larger arrays. The code sorts an array from a[start] to a[end] using the median-of-three technique. I believe...

recursive quicksort, count swap and comparison issue

c,arrays,recursion,quicksort,bubble-sort
I want to compare how many swaps and comparisons (<, >, ==, !=) it took for a bubblesort vs. quicksort function to sort an array of unique numbers. The problem is that the quicksort function I use is recursive and I am a bit unsure how to keep track of...

What is the mistake in this quicksort implementation?

algorithm,sorting,debugging,quicksort
The code works for a few test cases but fails for most. Where am I getting it wrong? Also if anything could be done to make the code more efficient. void quicksort(int *a, int a1, int b1) { if ((b1 - a1) > 0) { int pivot = (a1 +...

c# quicksort algorithm doesnt proceed

c#,arrays,algorithm,sorting,quicksort
I have just worked on implementation of quicksort in c# but then I have faced a such a problem. When I am using my function static void QS(int[] arr, int left, int right){ int pivot = left; int temp; int i = left + 1; int j = left +...

Quicksort index issue

c++,quicksort
I'm trying to implement basic quicksort function. I'm a little bit confused about index range. void q_sort(int * tab, int left, int right) { if(left < right) { int piv = left; for(int i = left+1; i <= right; ++i) if(tab[i] < tab[left]) change(tab[++piv], tab[i]); change(tab[piv],tab[left]); q_sort(tab, left, piv-1); q_sort(tab,...

How to get rid off the stackoverflows in my quicksort implementation?

java,quicksort,stack-overflow
everybody! I have got some stackoverflow problems with my quicksort implementation in Java with randomized pivot element for every recursive call for the quicksort as seen in the codes down below. My problem is that I have got stackoverflow at three (!) places in my codes: import java.util.Random; /** *...

Creating partition function for Quicksort in python

python,quicksort
Hi I am not able to create partition function for quicksort in python. I've searched on many websites but am not able to understand what is going on. I tried to do it but am stuck here. totalElem = input("Enter total Elements: ") c = 0 unsortElem = [0]*totalElem low...