Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quicksort with 3-way partition

What is QuickSort with a 3-way partition?

like image 968
CodeMonkey1313 Avatar asked Jun 02 '09 19:06

CodeMonkey1313


People also ask

What is 3 way partition quicksort?

In simple QuickSort algorithm, we select an element as pivot, partition the array around a pivot and recur for subarrays on the left and right of the pivot. Consider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}.

Is 3 way quick sort stable?

No, 3-way quick sort is also not stable.

Does quicksort use partition?

The basic algorithm. Quicksort is a divide-and-conquer method for sorting. It works by partitioning an array into two parts, then sorting the parts independently.

Which type of partitioning is used in quicksort?

Hoare Partitioning. Hoare partitioning was proposed by Tony Hoare when the Quicksort algorithm was originally published. Instead of working across the array from low to high, it iterates from both ends at once towards the center.


1 Answers

Picture an array:

3, 5, 2, 7, 6, 4, 2, 8, 8, 9, 0 

A two partition Quick Sort would pick a value, say 4, and put every element greater than 4 on one side of the array and every element less than 4 on the other side. Like so:

3, 2, 0, 2, 4, | 8, 7, 8, 9, 6, 5 

A three partition Quick Sort would pick two values to partition on and split the array up that way. Lets choose 4 and 7:

3, 2, 0, 2, | 4, 6, 5, 7, | 8, 8, 9 

It is just a slight variation on the regular quick sort.

You continue partitioning each partition until the array is sorted. The runtime is technically nlog3(n) which varies ever so slightly from regular quicksort's nlog2(n).

like image 181
jjnguy Avatar answered Sep 21 '22 21:09

jjnguy