-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksortstring.cpp
More file actions
56 lines (51 loc) · 884 Bytes
/
Copy pathquicksortstring.cpp
File metadata and controls
56 lines (51 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void swap(char*a,char*b){
char temp=*a;
*a=*b;
*b=temp;
}
int partition(int start,int end,string &arr){
char pivot=arr[start];
int count=0;
for(int i=start+1;i<=end;i++){
if(arr[i]<pivot){
count++;
}
}
int pivotindex=start+count;
swap(&arr[start],&arr[pivotindex]);
int i=start;
int j=end;
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 start,int end,string &arr){
if(start>=end){
return;
}
int pivot=partition(start,end,arr);
quicksort(start,pivot-1,arr);
quicksort(pivot+1,end,arr);
}
int main()
{
string arr;
cout<<"enter string";
cin>>arr;
int n=arr.length();
quicksort(0,n-1,arr);
cout<<arr;
}