Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
69 views

QuickSort Is A Divide and Conquer Algorithm

Quicksort is a divide and conquer algorithm that partitions an array around a pivot element. It recursively sorts subarrays of smaller elements before the pivot and larger elements after the pivot. The key process is partition, which places the pivot element in its final sorted position and rearranges the array so that elements smaller than the pivot come before it and larger elements come after. This is done in linear time to partition the array for further sorting of subarrays.

Uploaded by

Aakash Aggarwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

QuickSort Is A Divide and Conquer Algorithm

Quicksort is a divide and conquer algorithm that partitions an array around a pivot element. It recursively sorts subarrays of smaller elements before the pivot and larger elements after the pivot. The key process is partition, which places the pivot element in its final sorted position and rearranges the array so that elements smaller than the pivot come before it and larger elements come after. This is done in linear time to partition the array for further sorting of subarrays.

Uploaded by

Aakash Aggarwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

QuickSort is a Divide and Conquer algorithm.

It picks an element as pivot and partitions


the given array around the picked pivot. There are many different versions of quickSort
that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x.
All this should be done in linear time.
Pseudo Code for recursive QuickSort function :
/* low --> Starting index, high --> Ending index */

quickSort(arr[], low, high)

if (low < high)

/* pi is partitioning index, arr[p] is now

at right place */

pi = partition(arr, low, high);

quickSort(arr, low, pi - 1); // Before pi

quickSort(arr, pi + 1, high); // After pi

}
Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller
sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-
lists.
The steps are:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than the pivot come before the pivot,
while all elements with values greater than the pivot come after it (equal values can go either
way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
The base case of the recursion are lists of size zero or one, which never need to be sorted.

You might also like