0% found this document useful (0 votes)
33 views2 pages

Quick Sort

Quick sort is an efficient sorting algorithm that partitions an array into smaller arrays based on a pivot value, resulting in a worst-case running time of O(n²) but an average expected time of O(n log n). It employs a divide-and-conquer strategy, selecting a pivot, partitioning the array, and recursively sorting the subarrays. The algorithm is notable for its in-place sorting capability and is often the practical choice for sorting despite its worst-case scenario.

Uploaded by

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

Quick Sort

Quick sort is an efficient sorting algorithm that partitions an array into smaller arrays based on a pivot value, resulting in a worst-case running time of O(n²) but an average expected time of O(n log n). It employs a divide-and-conquer strategy, selecting a pivot, partitioning the array, and recursively sorting the subarrays. The algorithm is notable for its in-place sorting capability and is often the practical choice for sorting despite its worst-case scenario.

Uploaded by

Vipin chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Quick Sort

Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.
The quicksort algorithm has a worst-case running time of O(n2) on an input array of n numbers,
and worst case happens when the array is sorted in descending order. Despite a worst-case
running time, quicksort is often the best practical choice for sorting. Its average expected
running time is O(n log n), and it has an advantage of sorting in place.
Quicksort is a divide-and-conquer method for sorting.
It works as follows:
• Selection: A pivot element is selected.
• Divide: Parititon the array A[p..r] into two sub-arrays A[p..q-1] and A[q+1..r] such that
each element of A[p..q-1] <= A[q] <= A[q+1..r]
• Conquer: Sort the two subarrays A[p..q-1] and A[q+1..r] by recursive calls to quicksort
• Combine: Because the subarrays are already sorted, no work is needed to combine
them
Algorithm:
QuickSort Partitioning the array

void quick_sort(A,p,r) size_t Partition(A,p,r)


{ {
if (r - p > 1) i = p;
{ for (j = p; j < r-1; ++j)
q = Partition(A,p,r); {
quick_sort(A,p,q); if (A[j] <= A[r-1])
quick_sort(A,q+1,r); {
} swap(A[i],A[j]);
} ++i;
}
}
// the last place requires no test:
swap(A[i],A[r-1]);
return i;
}

As a procedure runs, it partitions the array into four regions.


Example 1:

You might also like