# 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 ...
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. ...
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 ...

### 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 ...
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 ...

### 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 ...
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 ...

### 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....

### 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 ...

### 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 ...
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 ...

### 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 ...
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 ...

### 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 ...

### 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 ...
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 ...

### Why does the recurrence equation for QuickSort consider all the elements in the array?

As well for the general case: [recurrence with $q$] Note how $q$ remains a free variable here; that's bad. It's not the same $q$ for every recursive call, and either way you can not really solve the ...
Accepted

### Why does the recurrence equation for QuickSort consider all the elements in the array?

When you're looking at the behaviour of the algorithm as $n\to\infty$, the difference between $n$ and $n-1$ becomes negligible. The simpler form is easier to deal with and it gives the same answer.
Accepted

### Proof that quicksort's running time is ∼1.39 n log n

it's claimed that the running time of QuickSort is $∼1.39\,n\log_2n$ I don't have the book handy, but it it most certainly does not claim this. That figure is the result of a close analysis of a very ...
Accepted

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.
### quicksort - Why is $\log_4 n$ used as an approximation instead of $\log_2 n$?
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 ...