Created
May 17, 2024 18:04
-
-
Save shitu13/46210d664fae492e51d521941d6e71a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
{ | |
public: | |
//Function to sort an array using quick sort algorithm. | |
void quickSort(int arr[], int low, int high) | |
{ | |
if(low>=high){ | |
return; | |
} | |
int pidx = partition(arr, low, high); | |
quickSort(arr, low, pidx-1); | |
quickSort(arr, pidx+1, high); | |
} | |
public: | |
int partition (int arr[], int low, int high) | |
{ | |
int pivot = arr[high]; | |
int pidx = low; | |
for(int i=low; i<high; i++){ | |
if(arr[i]<=pivot){ | |
swap(arr[i], arr[pidx++]); | |
} | |
} | |
swap(arr[pidx], arr[high]); | |
return pidx; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment