0% found this document useful (0 votes)
32 views3 pages

Quick Sort: Advantages

Quick sort is a divide and conquer algorithm that works by partitioning an array around a pivot value and recursively sorting the sub-arrays. It has average case performance of O(n log n) time but worst case of O(n^2) time. The algorithm chooses a pivot element and partitions the array by moving all smaller elements before it and larger elements after it, then recursively sorts the sub-arrays.

Uploaded by

santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Quick Sort: Advantages

Quick sort is a divide and conquer algorithm that works by partitioning an array around a pivot value and recursively sorting the sub-arrays. It has average case performance of O(n log n) time but worst case of O(n^2) time. The algorithm chooses a pivot element and partitions the array by moving all smaller elements before it and larger elements after it, then recursively sorts the sub-arrays.

Uploaded by

santosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Quick Sort

It is used on the principle of divide-and-conquer. Quick sort is an algorithm of choice in many

situations as it is not difficult to implement. It is a good general purpose sort and it consumes

relatively fewer resources during execution.

Advantages

 It is in-place since it uses only a small auxiliary stack.

 It requires only n (log n) time to sort n items.

 It has an extremely short inner loop.

 This algorithm has been subjected to a thorough mathematical analysis, a very precise

statement can be made about performance issues.

Disadvantages

 It is recursive. Especially, if recursion is not available, the implementation is extremely

complicated.

 It requires quadratic (i.e., n2) time in the worst-case.

 It is fragile, i.e. a simple mistake in the implementation can go unnoticed and cause it to

perform badly.
Quick sort works by partitioning a given array A[p ... r] into two non-empty sub array A[p ...

q] and A[q+1 ... r] such that every key in A[p ... q] is less than or equal to every key in A[q+1 ...

r].

Then, the two sub-arrays are sorted by recursive calls to Quick sort. The exact position of the

partition depends on the given array and index q is computed as a part of the partitioning

procedure.

Algorithm: Quick-Sort (A, p, r)


if p < r then
q Partition (A, p, r)
Quick-Sort (A, p, q)
Quick-Sort (A, q + r, r)

Note that to sort the entire array, the initial call should be Quick-Sort (A, 1, length[A])

As a first step, Quick Sort chooses one of the items in the array to be sorted as pivot. Then, the

array is partitioned on either side of the pivot. Elements that are less than or equal to pivot will

move towards the left, while the elements that are greater than or equal to pivot will move

towards the right.

Partitioning the Array

Partitioning procedure rearranges the sub-arrays in-place.

Function: Partition (A, p, r)


x ← A[p]
i ← p-1
j ← r+1
while TRUE do
Repeat j ← j - 1
until A[j] ≤ x
Repeat i← i+1
until A[i] ≥ x
if i < j then
exchange A[i] ↔ A[j]
else
return j

Analysis

The worst case complexity of Quick-Sort algorithm is O(n2). However using this technique, in

average cases generally we get the output in O(n log n) time.

You might also like