0% found this document useful (0 votes)
4 views

2 Quick Sort

The document provides an overview of the Quick Sort algorithm, detailing its divide and conquer approach through the steps of choosing a pivot, partitioning the array, and recursively sorting sub-arrays. It includes a C++ implementation of the algorithm and discusses its time complexity, noting best, average, and worst-case scenarios. Additionally, it highlights the importance of pivot selection in the efficiency of the sorting process.

Uploaded by

gul.zaib2012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2 Quick Sort

The document provides an overview of the Quick Sort algorithm, detailing its divide and conquer approach through the steps of choosing a pivot, partitioning the array, and recursively sorting sub-arrays. It includes a C++ implementation of the algorithm and discusses its time complexity, noting best, average, and worst-case scenarios. Additionally, it highlights the importance of pivot selection in the efficiency of the sorting process.

Uploaded by

gul.zaib2012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Superior University

Sargodha Campus

Advanced Algorithms
Assignment

Submitted to:

Dr. Naila Hamid

Submitted by:

Muhammad Arsalan

SU72-MSCSW-F24-004
Quick sort
Quick Sort works on the principle of divide and conquer, breaking down the problem into
smaller sub-problems.
There are mainly three steps in the algorithm:
1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can
vary (e.g., first element, last element, random element, or median).
2. Partition the Array: Rearrange the array around the pivot. After partitioning, all elements
smaller than the pivot will be on its left, and all elements greater than the pivot will be on
its right. The pivot is then in its correct position, and we obtain the index of the pivot.
3. Recursively Call: Recursively apply the same process to the two partitioned sub-arrays
(left and right of the pivot).
4. Base Case: The recursion stops when there is only one element left in the sub-array, as a
single element is already sorted.

Choice of Pivot

Always pick the first (or last) element as a pivot.

Pick a random element as a pivot.

Pick the median element is pivot

Algorithm: (in C++)

using namespace std;


int partition(vector<int>& arr, int low, int high) {

// Choose the pivot

int pivot = arr[high];

// Index of smaller element and indicates

// the right position of pivot found so far

int i = low - 1;

// Traverse arr[;ow..high] and move all smaller

// elements on left side. Elements from low to

// i are smaller after every iteration

for (int j = low; j <= high - 1; j++) {

if (arr[j] < pivot) {

i++;

swap(arr[i], arr[j]);

// Move pivot after smaller elements and

// return its position

swap(arr[i + 1], arr[high]);

return i + 1;
}

// The QuickSort function implementation

void quickSort(vector<int>& arr, int low, int high) {

if (low < high) {

// pi is the partition return index of pivot

int pi = partition(arr, low, high);

// Recursion calls for smaller elements

// and greater or equals elements

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

int main() {

vector<int> arr = {10, 7, 8, 9, 1, 5};

int n = arr.size();

quickSort(arr, 0, n - 1);

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";


}

return 0;

Output:

Sorted Array
1 5 7 8 9 10

Complexity Analysis of Quick Sort

Time Complexity:
 Best Case: (Ω (n log n)), Occurs when the pivot element divides the array into two equal
halves.
 Average Case (θ (n log n)), on average, the pivot divides the array into two parts, but not
necessarily equal.
 Worst Case: (O (n²)), Occurs when the smallest or largest element is always chosen as the
pivot (e.g., sorted arrays).
Auxiliary Space: O (n), due to recursive call stack

Quick sort recursive function:

It works by selecting a ‘pivot’ element from the array and partitioning the other elements into
two sub-arrays, according to selecting a ‘pivot’ element from the array and partitioning the
other elements into two sub-arrays, according to whether they are less than or greater than the
pivot.

You might also like