Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions C++/QuickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*Quicksort Complexity

Time Complexity
Best O(n*log n)
Worst O(n2)
Average O(n*log n)

Space Complexity O(n)
*/


#include<iostream>
using namespace std;


int partition( int arr[], int s, int e) {

int pivot = arr[s];

int cnt = 0;
for(int i = s+1; i<=e; i++) {
if(arr[i] <=pivot) {
cnt++;
}
}

//place pivot at right position
int pivotIndex = s + cnt;
swap(arr[pivotIndex], arr[s]);

//sorting left and right part of the pivot element
int i = s, j = e;

while(i < pivotIndex && j > pivotIndex) {

while(arr[i] <= pivot)
{
i++;
}

while(arr[j] > pivot) {
j--;
}

if(i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}

}

return pivotIndex;

}

void quickSort(int arr[], int s, int e) {

//base case
if(s >= e)
return ;

//performing partition
int p = partition(arr, s, e);

//sort the left part
quickSort(arr, s, p-1);

//sort right part
quickSort(arr, p+1, e);

}

int main() {

int arr[5] = {5,4,9,6,2};
int n = 5;

quickSort(arr, 0, n-1);

for(int i=0; i<n; i++)
{
cout << arr[i] << " ";
}
cout << endl;


return 0;
}