Skip to content

Commit a12fdb0

Browse files
authored
Merge pull request csubhasundar#185 from Abiral-2724/patch-1
Create Quicksort.cpp
2 parents 6f3e546 + 160c7e1 commit a12fdb0

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

c++/Quicksort.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
int partioning(int *arr,int s,int e){
5+
int pivot=arr[s];
6+
int count=0;
7+
for(int i=s+1 ; i<=e ; i++ ){
8+
if(arr[i]<=pivot){
9+
count++;
10+
}
11+
}
12+
// right index of pivot;
13+
int pivotindex=s+count;
14+
15+
swap(arr[s] , arr[pivotindex]);
16+
int i=s;
17+
int j=e;
18+
while(i<pivotindex && j>pivotindex){
19+
while(arr[i]<arr[pivotindex]){
20+
i++;
21+
}
22+
while(arr[j]>arr[pivotindex]){
23+
j--;
24+
}
25+
if(arr[i]>arr[pivotindex ] && arr[j]<arr[pivotindex ]){
26+
swap(arr[i] , arr[j]);
27+
i++;
28+
j--;
29+
}
30+
}
31+
return pivotindex;
32+
33+
}
34+
void quicksort (int arr[], int s, int e)
35+
{
36+
if (s >= e)
37+
{
38+
return;
39+
}
40+
int p=partioning(arr,s,e);
41+
//left bala part ko sort karo
42+
quicksort(arr,s,p-1);
43+
//right bala part ko sort karo
44+
quicksort (arr, p+1 , e);
45+
46+
}
47+
48+
int main ()
49+
{
50+
int arr[5] = { 2, 0, 1, 6, 9 };
51+
int n = 5;
52+
53+
quicksort(arr,0,n-1);
54+
for(int i=0 ; i<n ; i++){
55+
cout<<arr[i]<<" ";
56+
}
57+
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)