Resource Link
Quick Sort
Hasaner Rafkhata
Visualization
GeeksForGeeks
Implementation:
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<bits/stdc++.h> | |
using namespace std; | |
int n; | |
void print(int ar[]) | |
{ | |
for(int i = 0; i < n; i++){ | |
cout << ar[i] << " "; | |
} | |
cout << endl; | |
} | |
int partition(int A[], int start, int end) | |
{ | |
int i = start+1; | |
int piv = A[start]; | |
for(int j = start+1; j <= end; j++){ | |
if(A[j] < piv){ | |
swap(A[i], A[j]); | |
i++; | |
} | |
} | |
swap(A[start], A[i-1]); | |
return i-1; | |
} | |
void quick_sort(int A[], int start, int end) | |
{ | |
if(start < end){ | |
int piv_pos = partition(A, start, end); | |
quick_sort(A, start, piv_pos-1); | |
quick_sort(A, piv_pos+1, end); | |
} | |
} | |
int main() | |
{ | |
cin >> n; | |
int ar[n+5]; | |
for(int i = 0; i < n; i++){ | |
cin >> ar[i]; | |
} | |
quick_sort(ar,0,n); | |
print(ar); | |
return 0; | |
} |
No comments:
Post a Comment