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

Quick Sort

Quick Sort is an efficient sorting algorithm that uses a divide and conquer approach by selecting a pivot and partitioning the array into sublists. It recursively sorts the sublists until the base case is reached, with a time complexity ranging from O(n log n) to O(n^2). The algorithm involves swapping elements to ensure that all elements to the left of the pivot are less than or equal to it, and all elements to the right are greater.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Quick Sort

Quick Sort is an efficient sorting algorithm that uses a divide and conquer approach by selecting a pivot and partitioning the array into sublists. It recursively sorts the sublists until the base case is reached, with a time complexity ranging from O(n log n) to O(n^2). The algorithm involves swapping elements to ensure that all elements to the left of the pivot are less than or equal to it, and all elements to the right are greater.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Quick Sort

Quick Sort is one of the most efficient sorting algorithms and is based on the splitting of an array into
smaller ones.

The name comes from the fact that, quick sort is capable of sorting a list of data elements significantly
faster than any of the common sorting algorithms. And like Merge sort, Quick sort also falls into the
category of divide and conquer approach of problem-solving methodology.

Quick sort was invented by Tony Hoare.

Quicksort is sometimes referred to as partition exchange sort.

Quick sort picks one element as a pivot and shifts the pivot to the correct location in each iteration. By
repositioning the pivot, the list is divided into two sublists of equal or unequal size.
 Each sublist is solved in a recursive manner. In contrast to merge sort, the list is
partitioned dynamically in this case. Merge sort partitions an array based on its
position, whereas quicksort partitions it based on its actual value.

 Merge sort splits the list from the center, but quicksort does not guarantee such
balanced division. The value of the pivot, not the position, determines division.

 Pivot can be the first, last, or any random element can be chosen.

 Each sublist represents a new problem with a size smaller than n. A new pivot is
chosen, and the procedure is repeated until it reaches the base case (i.e. list of size
1).

 In Quick Sort, when performing the partition operation, if you find yourself in a
situation where i and j are equal, and there are no elements left to compare for i, it
generally means that you're at the point of placing the pivot into its correct position.
Here's how to handle that situation
Key Points:

Pivot Swap: If i is equal to j and you determine that you've finished comparing elements or that there
are no smaller elements than the pivot, it is customary to swap the pivot with i (or j, since they are
equal at this point). This places the pivot in its correct sorted position.

Context: This swap helps maintain the invariant of Quick Sort that ensures all elements to the left of the
pivot are less than or equal to it, and all elements to the right are greater.

Example Breakdown:

Start of Partitioning: You choose a pivot and initialize i and j.

Comparison Loop: You iterate with j, moving i only when you find an element smaller (or equal) to the
pivot.

Equal Indices: If i equals j and you've reached the end of possible comparisons (or the array segment
has been entirely processed), swapping the pivot with i is the correct action unless i and the pivot are
the same element.
Example Breakdown:

Start of Partitioning: You choose a pivot and initialize i and j.

Comparison Loop: You iterate with j, moving i only when you find an element smaller (or equal) to the
pivot.

Equal Indices: If i equals j and you've reached the end of possible comparisons (or the array segment
has been entirely processed), swapping the pivot with i is the correct action unless i and the pivot are
the same element.
O(n log n ) to O ( n2 )
Algorithm /Pseudocode of Quick Sort
function quickSort(array, low, high):
if low < high:
// Partition the array and get the pivot index
pivotIndex = partition(array, low, high)

// Recursively sort elements before and after partition


quickSort(array, low, pivotIndex - 1)
quickSort(array, pivotIndex + 1, high)

function partition(array, low, high):


// Choose the rightmost element as pivot
pivot = array[high]
i = low - 1 // Index of smaller element

for j from low to high - 1:


if array[j] <= pivot:
i=i+1 // Increment index of smaller element
swap(array[i], array[j]) // Swap

// Swap the pivot element with the element at i+1


swap(array[i + 1], array[high])
return i + 1 // Return the pivot index
void quickSort(int arr[], int low, int high) {
C PROGRAM TO IMPLEMENT QUICK SORT
if (low < high) {
#include <stdio.h>
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
void swap(int *a, int *b) {
quickSort(arr, pi + 1, high);
int temp = *a;
}
*a = *b;
}
*b = temp;
}
int main() {
int arr[] = {44,22,33,77,11,55,66};
int partition(int arr[], int low, int high) {
int n = sizeof(arr) / sizeof(arr[0]);
int pivot = arr[high];
quickSort(arr, 0, n - 1);
int i = (low - 1);
printf("Sorted array: \n");
for (int j = low; j < high; j++) {
for (int i = 0; i < n; i++)
if (arr[j] < pivot) {
printf("%d ", arr[i]);
i++;
return 0;
swap(&arr[i], &arr[j]);
}
}
OUTPUT:
}
swap(&arr[i + 1], &arr[high]);
Sorted array:
return (i + 1);
11,22,33,44,55,66,77
}

You might also like