Tag Info

Accepted

Why don't we use quick sort on a linked list?

The memory access pattern in Quicksort is random, also the out-of-the-box implementation is in-place, so it uses many swaps if cells to achieve ordered result. At the same time the merge sort is ...
• 9,495
Accepted

Finding k'th smallest element from a given sequence only with O(k) memory O(n) time

Create a buffer of size $2k$. Read in $2k$ elements from the array. Use a linear-time selection algorithm to partition the buffer so that the $k$ smallest elements are first; this takes $O(k)$ time. ...
• 3,380
Accepted

Trying to understand this Quicksort Correctness proof

We are indeed assuming $P(k)$ holds for all $k < n$. This is a generalization of the "From $P(n-1)$, we prove $P(n)$" style of proof you're familiar with. The proof you describe is known as the ...
• 14.9k

Can anyone give an example for worst case of quick sort if we employ median of three pivot selection?

If we employ quicksort by selecting the pivot as the median of three elements viz., the first element, the middle element and the last element, then when will the algorithm hit worst case? and also ...
• 119
Accepted

Stability of QuickSort Algorithm

One huge advantage of a stable sorting algorithm is that a user is able to first sort a table on one column, and then by another. Say that you have a website like Wikipedia with some tabular data, say ...
• 16.7k

Quicksort Partitioning: Hoare vs. Lomuto

Some comments added to the excellent Sebastian answer. I'm going to talk about the partition rearrangements algorithm in general and not about its particular use for Quicksort. Stability Lomuto's ...
Accepted

Hoare partitioning scheme in Quicksort

"The devil is in the details". Algorithms and programming, fortunately and unfortunately, needs the greatest attention to detail. The part of code that are critical here are the following ...
• 39k
Accepted

Isn't linear time O(n)?

Usually we call statement $A$ stronger than $B$ when $A$ implies $B$: $A \Rightarrow B$ (weaker-stronger). In other words, $B$ is weaker than $A$. When the presenter is speaking about linear time for ...
• 2,364

Trying to understand this Quicksort Correctness proof

This proof uses the principle of complete induction: Suppose that: Base case: $P(1)$ Step: For every $n > 1$, if $P(1),\ldots,P(n-1)$ hold (induction hypothesis) then $P(n)$ also holds....
• 278k
Accepted

Implementation of QuickSort to handle duplicates

The simple implementation idea is to separate the values into three groups: values less than the pivot, values equal to the pivot, and values greater than the pivot. In pseudocode, the algorithm ...
• 39k

What is the space complexity of quicksort?

Here is quicksort in a nutshell: Choose a pivot somehow. Partition the array into two parts (smaller than the pivot, larger than the pivot). Recursively sort the first part, then recursively sort the ...
• 278k

Why does Randomized Quicksort have O(n log n) worst-case runtime cost

You were missing that these texts talk about "worst case expected run time", not "worst case runtime". They are discussing a Quicksort implementation that involves a random element. Normally you ...
• 31.2k

Is there a sorting algorithm of order $n + k \log{k}$?

The short answer is no, in the worst-case comparison based algorithms, for reasons stated here. Using a counting technique will at least take $O(n \log n)$ worst case and $O(n \log k)$ if you use a ...
• 4,511
Accepted

Solving Recurrence Relation (quicksort )

Your mistake is $2\mathcal{O}(n/2) = \mathcal{O}(n/2)$. More generally, in order not to be confused, it is much better to replace $\mathcal{O}(n)$ with $An$ for some constant $A$, which can be taken ...
• 278k

Why don't we use quick sort on a linked list?

You can quick sort linked lists however you will be very limited in terms of pivot selection, restricting you to pivots near the front of the list which is bad for nearly sorted inputs, unless you ...
• 4,646

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

The worst case for the Quicksort algorithm depends how a pivot is chosen and it can range from $\Theta(n \log n)$ (if you choose the pivot to be the median) to $\Theta(n^2)$ (if the pivot is always a ...
• 29.6k
Accepted

Average number of exchanges during first partition stage in Quicksort

The partition method in the question, partition(a, lo, hi) is called Hoare’s partition scheme, which is the most classic partition scheme used in quicksort. Here ...
• 39k
Accepted

• 278k
Accepted

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

I could do QuickSelect (j - i) times to get all the elements Overkill. Two calls to QuickSelect and one linear pass are sufficient.
• 2,082

Why are unbalanced partitions worse than balanced partitions in Quicksort?

Raphael's answer gives you a formal proof: the recurrence from using a balanced partition solves to $\Theta(n \log n)$ and the (arbitrarily) unbalanced one to $\Theta(n^2)$. Quicksort's recursive ...
• 2,946

quicksort - Why is $\log_4 n$ used as an approximation instead of $\log_2 n$?

You should forward this question to the authors of the document you refer to. What the text you quote provides is intuition on the performance of quicksort on random inputs. Actual proofs use ...
• 278k