I am solving these problem using quick sort but it shows time limit exceeded,
but when i used direct sort(arg1,arg2) then it works, so what is the problem!
here is my code:
#include<bits/stdc++.h>
using namespace std;
void swapp(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}
void display(int *arr,int last)
{
for(int i=0;i<last;i++)
{
cout<<arr[i]<<endl;
}
}
int last_pivot_sort(int *arr, int beg, int last)
{
int pivot=arr[last];
int i=beg-1;
int j;
for(j=beg;j<last;j++)
{
if(arr[j]<pivot)
{
i++;
swapp(&arr[i],&arr[j]);
}
}
swapp(&arr[++i],&arr[last]);
return i;
}
void quicksort(int *arr,int beg,int last)
{
if(beg<last)
{
int m = last_pivot_sort(arr,beg,last);
quicksort(arr,beg,m-1);
quicksort(arr,m+1,last);
}
}
int main()
{
int T;
cin>>T;
int *arr=new int[T];
int i=0;
while(T–)
{
cin>>arr[i++];
}
quicksort(arr,0,i-1);
display(arr,i);
return 0;
}