int main(int argc, char const *argv[])
{
int array[1000000];
int i=0,num,j;
scanf("%d",&num);
while(num–)
{
scanf("%d",&array[i]);
i++;
}
quicksort(array,0,i-1);
for (j = 0; j < i ; ++j)
{
printf("%d\n",array[j]);
}
}
void quicksort(int *array,int lower, int upper)
{
int i;
if (lower<upper)
{
i=split(array,lower,upper);
quicksort(array,lower,i-1);
quicksort(array,i+1,upper);
}
}
int split(int *array_quicksort,int lower,int upper)
{
int a=lower+1;
int b=upper;
int i,temp;
i=array_quicksort[lower];
while(b>=a)
{
while(array_quicksort[a]<i)
{
a++;
}
while(array_quicksort[b]>i)
{
b--;
}
/*printf("----------check----------\n");
printf("%d %d\n",array_quicksort[a],array_quicksort[b]);*/
if (b>a)
{
/*temp=array_quicksort[a];
array_quicksort[a]=array_quicksort[b];
array_quicksort[b]=temp;*/
array_quicksort[a]=array_quicksort[a]^array_quicksort[b];
array_quicksort[b]=array_quicksort[a]^array_quicksort[b];
array_quicksort[a]=array_quicksort[a]^array_quicksort[b];
}
/*printf("----------check----------\n");
printf("%d %d\n",array_quicksort[a],array_quicksort[b]);*/
}
temp=array_quicksort[lower];
array_quicksort[lower]=array_quicksort[b];
array_quicksort[b]=temp;
/*SECTION 2 */
/*i=array_quicksort[b]^i;
array_quicksort[b]=array_quicksort[b]^i;
i=array_quicksort[b]^i;*/
/*SECTION 3 */
/*array_quicksort[lower]=array_quicksort[b]^array_quicksort[lower];
array_quicksort[b]=array_quicksort[b]^array_quicksort[lower];
array_quicksort[lower]=array_quicksort[b]^array_quicksort[lower];*/
return b;
}
I was trying to solve the TSORT problem intially I thought Quick sort was the solution to the problem but when I implemented it had shown the Time Limit Exceeded error so I thought to speed up the swapping by implementing it using the bitwise XOR operation I encountered 2 problems ::
Well firstly , it didnt work and I still have a TLE.
Secondly I tried to minimize the run time by using all the possibilities commented in Section 2 and Section 3.Because nothing was clicking me at that moment so i tried all the combinations something what poor programmers often do and yes I am guilty. But although both the snippets seems to be the same but they are giving me different results. I tried to trace the solution but I am not able to because of the looping constructs.