0% found this document useful (0 votes)
9 views1 page

Quick Sort

Quick Sort is a divide-and-conquer algorithm that recursively sorts an array by partitioning it around a pivot element and sorting the sub-arrays. It has average and best case time complexity of O(N log N) but worst case of O(N^2).

Uploaded by

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

Quick Sort

Quick Sort is a divide-and-conquer algorithm that recursively sorts an array by partitioning it around a pivot element and sorting the sub-arrays. It has average and best case time complexity of O(N log N) but worst case of O(N^2).

Uploaded by

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

Quick Sort:

Quick Sort is a divide-and-conquer technique that recursively sorts an array by


taking a pivot from the array and placing values lesser than the pivot to its left
and greater than the pivot to its right. The process is repeated until all the
elements in the array are sorted.

Divide: Quick Sort divides the array into two sub-arrays based on a chosen
pivot element.
Conquer: the two sub-arrays are recursively sorted.
Ultimately, we get a sorted array.

Time Complexity :
1. O(N*log N)
Occurs with perfect pivot choices leading to equal partitions.
2. O(N2)
Occurs with poor pivot choices leading to highly imbalanced partitions.

<<void quickSort(int[] array,int lowIndex,int highIndex) {


if(lowIndex>=highIndex) {
return;
}
int pivot = array[highIndex];
int leftPointer = lowIndex;
int rightPointer = highIndex;
while(leftPointer < rightPointer) {
while(array[leftPointer]<=pivot && leftPointer<rightPointer) {
leftPointer++;
}
while(array[rightPointer]>=pivot && leftPointer<rightPointer) {
rightPointer--;
}
swap(array,leftPointer,rightPointer);
}
swap(array,leftPointer,highIndex);
quickSort(array,lowIndex,leftPointer-1);
quickSort(array,leftPointer+1,highIndex);
}
void swap(int[] array,int index1,int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
>>

You might also like