You are on page 1of 11

QuickSort

QuickSort is an efficient, general-purpose sorting algorithm


developed by British computer scientist Tony Hoare in 1959. It
was published in 1961 and remains widely used for sorting
tasks. This divide-and-conquer algorithm operates by selecting
a 'pivot' element from the array, partitioning the other
elements into two sub-arrays based on their relationship to the
pivot, and then recursively sorting these sub-arrays. The
algorithm is known for its speed, particularly on larger datasets,
and its ability to perform in-place sorting, requiring only small
additional amounts of memory. QuickSort is a comparison sort,
meaning it can sort items of any type for which a "less-than"
relation (total order) is defined. While most implementations of
QuickSort are not stable, implying that the relative order of
equal items may not be preserved, it has gained widespread
adoption and influenced library sort subroutines in languages
such as C and Java. The algorithm has undergone refinements
and improvements over the years, contributing to its continued
relevance in the field of computer science.
Advantages of Quicksort:

- Efficiency: Quicksort proves to be a highly efficient algorithm,


demonstrating its capability to rapidly sort extensive datasets.

- Memory Efficiency: Being an in-place sorting algorithm,


quicksort is notably efficient in terms of memory usage.

- Versatility: Quicksort exhibits versatility by being applicable to


a diverse range of data types.

Disadvantages of Quicksort:

- Worst-Case Time Complexity: Quicksort's time complexity in


the worst-case scenario is O(n^2), a rarity that can impact its
performance negatively.

- Sensitivity to Pivot Selection: The effectiveness of quicksort is


dependent on the careful selection of the pivot element. Poor
choices may result in suboptimal performance.

- Recursion Overhead: Quicksort, relying on recursion,


introduces overhead in the form of additional stack frames,
contributing to computational costs.
Quick Sort Algorithm

Sorting involves organizing items systematically. Quicksort, a


widely utilized algorithm, conducts approximately n log n
comparisons in the average case for sorting an array with n
elements. Recognized for its speed and high efficiency, this
algorithm adheres to the divide-and-conquer approach. This
technique entails breaking down algorithms into subproblems,
solving them, and then integrating the results to address the
original problem.

 Divide:

In the division phase, a pivot element is selected first.


Subsequently, the array is partitioned or rearranged into two
sub-arrays. Each element in the left sub-array is less than or
equal to the pivot element, while each element in the right sub-
array is larger than the pivot element.

 Conquer:

Recursively, two subarrays are sorted using Quicksort.


 Combine:

The already sorted arrays are combined.

Quicksort designates an element as a pivot and partitions the


array around this chosen pivot element. In this process, a large
array is split into two arrays, with one containing values smaller
than the specified pivot, and the other holding values greater
than the pivot.

This partitioning continues for both left and right sub-arrays


until only single elements remain in each sub-array.
 Selecting the Pivot

Selecting an effective pivot is crucial for the efficient execution


of quicksort. Nevertheless, determining what constitutes a
good pivot is a common challenge. Several methods for
choosing a pivot include:

1. Opting for a random pivot, which involves selecting a pivot at


random from the given array.

2. Choosing either the rightmost or leftmost element of the


array as the pivot.

3. Selecting the median as the pivot element.

 Quicksort Efficiency Analysis

1. Time Complexity:
- Optimal Case: Quicksort performs best when the pivot
element is centrally positioned or close to the center, exhibiting
a time complexity of O(n*logn).

- Average Case: When the array elements lack a specific


order, falling into a random and unsorted arrangement,
quicksort maintains an average time complexity of O(n*logn).

- Worst Case: The most challenging situation arises when the


pivot element is the largest or smallest in the array. For
instance, consistently having the last element as the pivot in an
already sorted array incurs a worst-case time complexity of
O(n^2). Despite this seemingly higher complexity compared to
alternatives like Merge sort and Heap sort, practical usage
often demonstrates quicker performance. The worst-case
scenario can be alleviated through prudent pivot selection.

2. Space Complexity:

- Quicksort's space complexity is denoted as O(n*logn).

How to do quick sorting with illustration

1. Select the pivot


The pivot is an arbitrary element from the unsorted list. In
this example, we will choose the middle element, 63, as
the pivot.

2. Partition the list

Partitioning involves rearranging the list so that all


elements less than the pivot are placed to its left, and all
elements greater than the pivot are placed to its right.

Starting from the right side of the list, move elements


smaller than the pivot to the left until you encounter an
element larger than the pivot. Swap the two elements.

Continue this process until you reach the leftmost


element. If you encounter an element larger than the
pivot, swap it with the pivot. This effectively places the
pivot in its correct position in the sorted list.

3. Recursively sort the sublists

After partitioning, the list is divided into two sublists: one


containing elements less than the pivot, and the other
containing elements greater than the pivot. Recursively
apply the Quicksort algorithm to these sublists.

In this example, the sublists are

23 8 28 13 12 18 26

Select the pivot.

23 8 28 13 12 18 26

Put the smalls on the left and bigs on the right.

23 8 13 12 18 26 28
Continue recursively.

23 8 13 12 18

8 13 12 18 24

8 12 13 18 24

Finish.

8 12 13 18 24 26 28
Quick Sorting program in Python.

You might also like