Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickselect Algorithm - Simplified Explanation

I've asked this before HERE, however I wish to have the explanation of quickselect (based on quicksort) simplified further. The previous question I asked included some example code (so you know what I'm on about).

I was wondering if anyone anywhere at any point had summarised the rules and guidelines of quickselect as a game, where one can learn how the algorithm works by following easily understood rules that can be applied to let's say a deck of cards or numbers on bits of paper.

I think a simplified explanation of the quickselect algorithm would be paramount to me understanding how it works, as still the tutorials and explanations I've received are difficult to grasp and visualise. Even the videos on youtube that turns quicksort into a dance haven't helped greatly.

Thanks in advance Stack, you've been a great help so far.

like image 531
Edge Avatar asked Jun 02 '12 09:06

Edge


People also ask

How does quick select algorithm work?

Quickselect uses the same overall approach as Quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot.

What is quickselect in Java?

Quickselect is a selection algorithm to find the k-th smallest element in an unordered list. It is related to the quick sort sorting algorithm. Examples: Input: arr[] = {7, 10, 4, 3, 20, 15} k = 3 Output: 7 Input: arr[] = {7, 10, 4, 3, 20, 15} k = 4 Output: 10.

What is the best running time of quickselect?

The time complexity for the average case for quick select is O(n) (reduced from O(nlogn) — quick sort). The worst case time complexity is still O(n²) but by using a random pivot, the worst case can be avoided in most cases.

Is quick select Same as quick sort?

The algorithm is similar to QuickSort. The difference is, instead of recurring for both sides (after finding pivot), it recurs only for the part that contains the k-th smallest element.


1 Answers

You walk into a gymnasium containing 200 children. It is September 8th, so you have a burning desire to find the 98th shortest child. You know that you could line them all up from shortest to tallest, but that would take forever. "I know", you think, "I could use QUICKSELECT!"

You walk out into the crowd, close your eyes, stick out your finger, and spin around three times. When you open your eyes, you are pointing directly at one of the children, Peter Pivot. You say, "Quickly! Everybody shorter than Peter, come stand over here. And everybody taller than Peter, go stand over there. If you're the same height as Peter, you can go into either group."

The children shuffle around, and soon they are standing in the two groups. You count and find 120 children in the shorter group, and 79 children in the taller group. You know the 98th shortest child must be in the shorter group, so you tell Peter and the 79 taller children to sit in the bleachers.

You again close your eyes, stick out your finger, and spin around three times. When you open your eyes, you are pointing directly at Peter's sister, Paula Pivot. You say, "Quickly! Those of you who are still standing. If you're shorter than Paula, come stand over here. If you're taller than Paula, go stand over there. If you're the same height, you can go into either group."

The children shuffle around, and soon they are standing in the two groups. You count and find 59 children in the shorter group, and 60 children in the taller group. You know the 98th shortest child must be in the taller group, so you tell Paula and the 59 shorter children to sit in the bleachers.

You again close your eyes, stick out your finger, and spin around three times. When you open your eyes, you are pointing directly at Paula's cousin, Prudence Pivot. You say, "Quickly! Those of you who are still standing. If you're shorter than Prudence, come stand over here. If you're taller than Prudence, go stand over there. If you're the same height, you can go into either group."

The children shuffle around, and soon they are standing in the two groups. You count and find 37 children in the shorter group, and 22 children in the taller group. You know that Paula and 59 other shorter children are sitting on the bleachers. Along with the 37 shorter children still standing, you know that a total of 97 children are shorter than Prudence. Therefore, Prudence is the 98th shortest child!

Quickselect FTW!

like image 61
Chris Okasaki Avatar answered Sep 22 '22 16:09

Chris Okasaki