1035 | CodeChef

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;
}

Any inbuilt function is extremely efficient, so it will always take lesser time to execute than if you manually type out the same logic.
Not sure if the author wanted you to use the inbuilt function or not though

1 Like

Generate a testcase with T=1000000 and then the same number repeated 1000000 times.

See how long your implementation takes to process it.

thankyou for your response.