Questions tagged [quicksort]

Sorting algorithm based on recursive partitioning devised by Hoare (ACM Algorithm 63) with fast average case running time.

Filter by
Sorted by
Tagged with
1 vote
1 answer
43 views

Quicksort for Strings

I am currently trying to study the Quicksort algorithm. I understand how it works for integers but can anyone explain how does Quicksort algorithm work with strings? The reason for my confusion is ...
user avatar
0 votes
2 answers
24 views

Searching for sorting algorithm taking into account all possible solution of similar numbers

I need a reference for sorting algorithm where all possible orders are considered. example: if we have four values of n, and we do know there values n1(3) n2(5) n3(5) n4(10) and want to order them in ...
user avatar
0 votes
0 answers
16 views

change an algo to obtain optimal run time

I have an algorithm that does the reverse of partition ...
user avatar
  • 35
1 vote
0 answers
41 views

Median as pivot selection halves array into one third and two thirds

Selecting the median as an approach for pivot selection halves the array into $T(\frac n3)$ and $T(\frac{2n}{3})$, so our $T(n)$ becomes: $$T(n) = T(\frac{2n}{3})+ T(\frac n3)$$ Solution: Approach 1: ...
user avatar
  • 459
4 votes
2 answers
973 views

Stability of QuickSort Algorithm

Def: the stability of algorithm is defined in case of the algorithm preserves same value elements while sorting as the following shows: So for this QuickSort algorithm: ...
user avatar
  • 459
0 votes
2 answers
154 views

Complexity of sorting $k$-sorted array using QuickSort and HeapSort

Given a $k$-sorted array where each element in the array is $k$ positions from its correct position, we want to sort such array using quick sort. Generally speaking, I understand that running time is ...
user avatar
  • 459
1 vote
1 answer
21 views

For QuickFind, which sub problem we should consider

Question: Suppose we have the following array where we want to find the smallest $i$th smallest element in the array using QuickFind algorithm (similar to QuickSort): $$ QuickFind\left( A,n,i \right) ...
user avatar
  • 459
1 vote
1 answer
36 views

In a sorted subarray $Z_{ij}$ elements $i$ and $j$ get compared when either $i$ or $j$ is pivot

This is related to the discussion of average case of quick sort. Given that we have a sorted sub-array $Z_{ij} = i, i+1, \dots, j$ where $i < j$. Claim: $i$ and $j$ are compared if and only if, ...
user avatar
  • 459
0 votes
0 answers
37 views

What is the value returned by Hoare partitioning?

Following the article about Quicksort from Wikipedia, there is an implementation of Hoare partitioning: ...
user avatar
  • 1
1 vote
1 answer
74 views

Pivot algorithm in median of medians

The pseudocode for the pivot function for the Median of Medians algorithm is given in https://en.wikipedia.org/wiki/Median_of_medians. On the last line, there is a ...
user avatar
2 votes
3 answers
2k views

Time Complexity of Quicksort when using Median Pivot in sorted array

I am looking for any literature or reference for the worst case complexity of using quicksort on a sorted array with median as pivot. Different internet sources give conflicting answers and often skip ...
user avatar
2 votes
1 answer
86 views

Hoare partition scheme may cause infinite recursion

Wiki states: "...partitioning algorithm guarantees lo ≤ p < hi which implies both resulting partitions are non-empty, hence there's no risk of infinite recursion." What prevents Hoare ...
user avatar
  • 23
0 votes
3 answers
108 views

Runtime of sorting algorithms given a particular input

say that we have {2,3,5,4,6} as input that we want to sort in ascending order. Then, we know that we can use any of the sorting algorithms: bubble, insertion, selection, quick, merge, heap or counting....
user avatar
2 votes
1 answer
174 views

Understanding the upper bound proof for quick sort

I'm trying to understand the average run time of quicksort which is $O(n \log n)$. I understand the intuition behind it: if we partition array $A$ to e.g. $\alpha n $ and $(1-\alpha)n$ then we ...
user avatar
  • 123
1 vote
1 answer
28 views

Is there a way to find the correct element in the array for the given index x?

In quick sort, in each iteration we are able to find correct index for an element (i.e. pivot element). Is there any algorithm to find correct element for a given index ? Here, correct index of an ...
user avatar
  • 113
0 votes
1 answer
40 views

How to arrange a sub-array for Quick sorting algorithm?

Alghorithm : Quick sort . idea : devide and conqure . steps : 1- find the pivot point from array like first element . 2- partiotioning the array so that elements are smaller than pivot point are in ...
user avatar
  • 19
-1 votes
1 answer
98 views

Number of comparisons in Quicksort

So would it be correct to say that the number of comparisons from level 1 to level 2 would be $2(n/2-1)$? Or would it be more correct to say that the number of comparisons is $2^i(n/2^i-1)$?
user avatar
  • 1
2 votes
2 answers
56 views

Quicksort: Probability of an element being compared to fewer than $k$ pivot elements

Assume we want to use quicksort on some array $s$ with length $n$ consisting of only $n$ distinct elements. Let $S_{(1)},S_{(2)},\dots,S_{(n)}$ be the sorted order of the elements in $S$. Furthermore, ...
user avatar
0 votes
1 answer
200 views

Quick sort, Hoare's partition algorithm. Is there a mistake in CLRS?

The following problem appears in "Introduction to Algorithms" by Thomas Cormen et. al., aka CLRS. Problem 7-1.b Hoare's partition algorithm from the book. Part b: Assuming the subarray $A[p,...
user avatar
2 votes
1 answer
101 views

Proving Quicksort is $O(n^2)$

So I'm trying to figure out why the worst case of Quicksort is $O(n^2)$. I know this a very well known problem, but the funny thing is where ever I look (even Wikipedia) gives the following ...
user avatar
0 votes
2 answers
317 views

no of times partion is called in quick sort, assuming array is always halved

In the most even possible split, PARTITION function produces two subarrays, each of size no more than n/2. since one is of size floor(n/2) and one of size ceil(n/2)-1. The recurrence for the running ...
user avatar
-4 votes
1 answer
32 views

Run time for first and last index as a pivot in Quicksort?

What is the running time of quicksort if we always use the first index as the pivot? What if we always use the last index as the pivot? Is the running time $O(n)$?
user avatar
  • 1
1 vote
2 answers
942 views

What is the space complexity of quicksort?

What is the space complexity of quicksort? I was doing some research and found some saying it is $O(1)$, some saying it's $O(\log n)$, and some saying $O(n)$. Not sure what to believe, even though $O(\...
user avatar
  • 11
1 vote
2 answers
454 views

How does size of list in merge-sort, quick-sort, insertion-sort, matter?

We have been taught that: Insertion-sort will best work if we have a small list. Quick-sort will best work if we have a long list. Merge-sort will best work if we have a huge list. It is not ...
user avatar
1 vote
1 answer
58 views

Quickly determine if insertion sort or quick sort is better

I'm in a scenario where ~30% of the time, my array is almost completely sorted, and the other 70% of the time, it is basically completely random. I want to quickly determine if my list is almost ...
user avatar
0 votes
2 answers
66 views

How to known which algorithm is the best for what situation, when sorting numbers?

Is there some kind of "universal list" of performance of different algorithms in different situations? I have different databases that save user input (numbers). However some of these sets ...
user avatar
  • 111
0 votes
2 answers
154 views

Average case running time of quick sort

How to show that the quick-sort algorithm runs in $O(n^2)$ time on average ? Because on average, the expected running time is in $O(n\log n)$. The algorithm should not be in exponential time.
user avatar
1 vote
0 answers
55 views

QuickSort when the range of data is known

In QuickSort Algorithm, the pivot is chosen as the first element or a randomised element. However, if the range of data to be sorted is known, For example, from 1 to 100, and they are mostly equally ...
user avatar
8 votes
2 answers
2k views

Isn't linear time O(n)?

In the question in this video about quicksort luckily picking the median in each recursive call. Tim Roughgarden, the presenter, says at 11:22 Partition needs really linear time, not just $O(n)$ time....
user avatar
0 votes
1 answer
508 views

Solving the recursive equation $T(n)=T(k)+T(n-k-1)+O(n)$

The question is clear in the title. I am trying to solve this recursion as a part of showing that the worst case of quicksort algorithm occurs when $k=0$, but can't do it. I could do the following ...
user avatar
  • 121
1 vote
0 answers
37 views

Running time of random pivot quicksort on random and sorted arrays

I don't understand why I am getting the following execution times for the quicksort with a random pivot. Times are in microseconds they are the average of five executions. Random array: ...
user avatar
-1 votes
1 answer
274 views

Time complexity of a machine which combines Insertion Sort and Quicksort

Given a machine that sorts an array of length $n$ with the following algorithm: Sort first $2\sqrt{n} + 1$ elements of array with Insertion Sort.(Check Insertion Sort) Select the median of the whole ...
user avatar
0 votes
0 answers
27 views

How does quicksort handle case when we choose the first number as pivot and all the remaining elements after the pivot is greater

For example, we have an array [4,7,6,13] and I choose the first item 4 as the pivot. Now I have a pointer i that goes through the array from index 1 to index 3; and I have another pointer j that ...
user avatar
  • 1
0 votes
0 answers
96 views

Calculating the running time of Quicksort's PARTITION procedure

I am confused about calculating the PARTITION procedure's running time. PARTITION procedure is used in the Quicksort Algorithm to partition the array $A[p...r]$ I analyzed the PARTITION procedure ...
user avatar
1 vote
0 answers
106 views

Clarification of the analysis of the worst case situation of quicksort as dealt with in CLRS

I was going through the text Introduction to Algorithms by Cormen et. al. and I came across their analysis of the worst case of the quicksort algorithm. I could not quite understand a few specific ...
user avatar
2 votes
1 answer
443 views

Average number of exchanges during first partition stage in Quicksort

I am trying to understand average no of exchanges in Quicksort. Here is the code to partition the array - ...
user avatar
1 vote
1 answer
87 views

Quicksort Time Complexity

I am learning the Quicksort algorithm and I am struggling with understanding the time complexity. Here is the JavaScript ES6 code for the partition function that is used in the algorithm: ...
user avatar
1 vote
2 answers
774 views

Show that the best case time complexity of Quicksort is $\Omega(n \log n)$

I am trying to show that the best case time complexity of Quicksort is $\Omega(n \log n)$. The following recurrence describes the best-case time complexity of Quicksort: $$T(n) = \min_{0 \le q \le n-...
user avatar
  • 9,209
1 vote
2 answers
348 views

quicksort invariant 3 conditions with loop invariant

in studying Quicksort using the book "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein, they describe in order to show correctness, an invariant must hold for the 3 stages of the ...
user avatar
0 votes
1 answer
87 views

Radix sort slower than Quick sort?

I would like to demonstrate that sometime radix-sort is better than quick-sort. In this example I am using the program below: ...
user avatar
  • 219
0 votes
0 answers
107 views

Improving QuickSort Algorithm with pivot as first element

I was trying to improve the algorithm since its the most effective and known algorithm among many others, I came across " Quicksort algorithm with an early exit for sorted subfiles 1987 by University ...
user avatar
1 vote
1 answer
434 views

Quick Sort vs Radix Sort

In an coding exam, I was once asked this question: Assuming that you are only sorting Integers in ascending order, which algorithm do you use when you want to prioritize speed the most, but you ...
user avatar
1 vote
2 answers
2k views

Probability that two elements are compared in randomized quicksort

I am having an issue in a specific part of the randomized quick-sort analysis. As per the randomized quick-sort algorithm the pivot is chosen from the given subset on which it is called from a random ...
user avatar
1 vote
2 answers
2k views

Analysing worst-case time complexity of quick-sort in different cases

I am trying to understand worst case time complexity of quick-sort for various pivots. Here is what I came across: When array is already sorted in either ascending order or descending order and we ...
user avatar
  • 215
0 votes
2 answers
77 views

Why guess $\Theta(n^2)$ for the substitution method of worst-case partitioning

In the book Introduction to Algorithms (3th edition) chapter 7 the recurrence of the running time of quicksorts partitioning is given by $$T(n) = T(n-1) + \Theta(n)$$ as the worst-case happens ...
user avatar
  • 343
2 votes
0 answers
68 views

How to predict the number of comparisons done by QuickSort if you know the percentage to which the array is pre-sorted?

I've noticed that correlating the number of comparisons done by a naive implementation of QuickSort with the percentage of elements that were already sorted gives you a curly-brace-shaped-curve if you ...
user avatar
1 vote
2 answers
458 views

Quick Sort Equal to or Less Than

For my course I have to memorise a number of algorithms and to know how to perform them by hand. The steps of the quick sort are given as the following in the text book I am using: Choose the item at ...
user avatar
  • 111
0 votes
1 answer
561 views

What is the worst case for C++ "sort" function?

So, what is the worst case for C++ "sort" function, when does it go to O(n^2) time? I know it's QuickSort, therefore, it's very fast in most cases, but it gets to O(n^2) in special cases. I've tried ...
user avatar
0 votes
1 answer
496 views

What is the probability of comparision between smallest and greatest element in array when quick sort randomly choose the pivot element?

Consider the recursive quick sort with random pivoting i.e. each time a random pivot element is chosen uniformly. When this ...
user avatar
1 vote
2 answers
112 views

Algorithm Design: Efficient O(n) algorithm to get the ith to jth largest elements in an array

I am trying to design an efficient algorithm that retrieves the ith to jth largest elements in an array. For example, if the following array is the input: ...
user avatar
  • 125