Let's begin!
Quick Sort is a sorting algorithm which follows the principle of Divide and Conquer with average time complexity as n(log n) and worst time complexity as n^2.
We have two functions to implement Quick Sort:
Quick Sort Functions
- quickSort( ) Recursively call the function until start is less than end.
- partition( ) Swap elements to left which are less than pivot and brings pivot to it's ideal position.
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
#include <iostream> | |
using std::endl; | |
using std::cin; | |
using std::cout; | |
void display(int arr[], int length) | |
{ | |
for(int i=0; i<length; i++) | |
{ | |
cout<<arr[i]<<" "; | |
} | |
cout<<endl; | |
} | |
void swap(int *num1, int *num2) | |
{ | |
int temp = *num1; | |
*num1 = *num2; | |
*num2 = temp; | |
} | |
int partition(int arr[], int start, int end) | |
{ | |
int pivot = arr[end]; | |
int initial = start-1; | |
for (int i=start; i<=end-1; i++) | |
{ | |
if(arr[i] < pivot) | |
{ | |
swap(&arr[++initial], &arr[i]); | |
} | |
} | |
swap(&arr[++initial], &arr[end]); | |
return initial; | |
} | |
void quickSort(int arr[], int start, int end) | |
{ | |
if(start<end) | |
{ | |
int pivot_index = partition(arr, start, end); | |
quickSort(arr, start, --pivot_index); | |
quickSort(arr, ++pivot_index, end); | |
} | |
} | |
int main() | |
{ | |
int length; | |
cout<<"Length of the Array: "; | |
cin>>length; | |
int arr[length]; | |
for(int i=0; i<length; i++) | |
{ | |
cout<<"Enter Element "<<(i+1)<<" : "; | |
cin>>arr[i]; | |
} | |
cout<<"Given Array: "; | |
display(arr, length); | |
quickSort(arr, 0, length-1); | |
cout<<"Sorted Array: "; | |
display(arr, length); | |
return 0; | |
} |
Comments
Post a Comment