You are on page 1of 6

#include<bits/stdc++.

h>
#include<string>
#include<cmath>
using namespace std;

void As_merge_array(char arr[], int low, int mid, int high)


{
int n1=mid-low+1;
int n2=high-mid;
int left[n1], right[n2];

for(int i=0; i<n1; i++)


{
left[i]=arr[low+i];
}
for(int i=0; i<n2; i++)
{
right[i]=arr[mid+i+1];
}
int i=0,j=0,k;
k = low;
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = left[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = right[j];
j++;
k++;
}
}

void As_mergeSort(char arr[], int low, int high)


{
if(low<high)
{
int mid=(high+low)/2;
As_mergeSort(arr, low, mid);
As_mergeSort(arr, mid+1, high);
As_merge_array(arr, low, mid, high);
}
}
void De_merge_array(char arr[], int low, int mid, int high)
{
int n1=mid-low+1;
int n2=high-mid;
int left[n1], right[n2];

for(int i=0; i<n1; i++)


{
left[i]=arr[low+i];
}
for(int i=0; i<n2; i++)
{
right[i]=arr[mid+i+1];
}
int i=0,j=0,k;
k = low;
while (i < n1 && j < n2)
{
if (left[i] >= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = left[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = right[j];
j++;
k++;
}
}

void De_mergeSort(char arr[], int low, int high)


{
if(low<high)
{
int mid=(high+low)/2;
De_mergeSort(arr, low, mid);
De_mergeSort(arr, mid+1, high);
De_merge_array(arr, low, mid, high);
}
}

int A_partition(char ar2[],int first, int last)


{
int p=ar2[last];
int pi = first;
for(int i=first; i<last; i++)
{
if(ar2[i]<=p)
{
swap(ar2[pi], ar2[i]);
pi++;
}
}
swap(ar2[last], ar2[pi]);
return pi;
}
void As_quickSort(char ar2[],int low, int high)
{
if(low<high)
{
int index = A_partition(ar2,low, high);
As_quickSort(ar2,low, index-1);
As_quickSort(ar2,index+1, high);
}
}

int D_partition(char ar2[],int first, int last)


{
int p=ar2[last];
int pi = first;
for(int i=first; i<last; i++)
{
if(ar2[i]>=p)
{
swap(ar2[pi], ar2[i]);
pi++;
}
}
swap(ar2[last], ar2[pi]);
return pi;
}
void D_quickSort(char ar2[],int low, int high)
{
if(low<high)
{
int index = D_partition(ar2,low, high);
D_quickSort(ar2,low, index-1);
D_quickSort(ar2,index+1, high);
}
}
int main()
{
char arr[100];
cout<<"Input your string "<<endl;
cin>>arr;
cout<<endl;
cout<<arr<<endl;
cout<<"\n";
int length=strlen(arr);
int le=round(length/2);
int l2=length-le;
char ar1[le],ar2[length-le];
int i,j,p;
for( j=0; j<le; j++)
{
ar1[j]=arr[j];
}
for( i=0; i<=length-le; i++)
{
ar2[i]=arr[j];
j++;
}

cout<<ar1<<" "<<" This part will be Sorted by Merge Sort \n"<<endl;


cout<<ar2<<" "<<" This part will be Sorted by Quick Sort \n"<<endl;

int n1;
cout<<"\nFor assending press 1\nFor dissending press 2\n";
cin>>n1;
if(n1==1)
{
As_mergeSort(ar1, 0, le-1);
for(int i=0; i<le; i++)
{
cout<<ar1[i];
}
cout<<"\n";
}
else if(n1==2)
{
De_mergeSort(ar1, 0, le-1);
for(int i=0; i<le; i++)
{
cout<<ar1[i];
}
cout<<"\n";
}

cout<<"\nFor assending press 1\nFor dissending press 2\n";


cin>>n1;
if(n1==1)
{
As_quickSort(ar2,0, l2-1);
for(int i=0; i<l2; i++)
{
cout << ar2[i];
}
cout<<"\n";
}
else if(n1==2)
{
D_quickSort(ar2,0, l2-1);
for(int i=0; i<l2; i++)
{
cout << ar2[i];
}
cout<<"\n";
}

else
cout<<"Wrong input ! Try again."<<endl;

return 0;
}

You might also like