TURBO SORT


#1

#include
using namespace std;
int main()
{
int t,a[100],s;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>a*;
}
for(int j=0;j<t;j++)
{
for(int i=j+1;i<t;i++)
{
if(a[j]>a*)
{
s=a*;
a*=a[j];
a[j]=s;
}
}
}
for(int i=0;i<t;i++)
{
cout<<a*<<"
"<<endl;
}
return 0;

}
WHAT IS WRONG?????????????????????????????

#2

a[100]

The input surely has more than 100 elements. This causes you to go out of boudns heavily (out of index exception). This gives runtime error or WA.

Also, even if you correct that, you will get TLE. Give a read to inbuilt sort function. :slight_smile:


#3

Your edit code don’t initialize arrays with random numbers in most cases

#include

using namespace std;

int main()

{

int t,s;

cin >> t;

int a[t];

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

{

    cin >> a*;

}

for(int j=0;j<t-1;j++)

{

    for(int i=j+1;i<t;i++)

    {

        if(a[j]>a*)

        {

            s=a*;

            a*=a[j];

            a[j]=s;

        }

    }

}

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

{

    cout<<a*<<endl;

}

return 0;

}


#4

This sort is also known as Bubble sort, which you can read in detail alongwith implementation in C [here][1].

By the way, bubble sort in perhaps the slowest sort algorithm with O(n*n) complexity.

So if you wanna use a sort algorithm in a programming contest, you ought to look for [MergeSort][2], [HeapSort][3], [QuickSort][4]

Or you may find about inbuilt sort function in your language which is usually fastest in most of cases…

Please UPVITE and Accept, if you find this helpful…
[1]: http://www.geeksforgeeks.org/bubble-sort/
[2]: http://www.geeksforgeeks.org/merge-sort/
[3]: http://www.geeksforgeeks.org/heap-sort/
[4]: http://www.geeksforgeeks.org/quick-sort/


#5

So, this question wants you to use the inbuilt sort function?


#6

Alternatively, if you are intuitive enough, you can make the simple O(N) sorting algorithm if value of elements dont exceed {10}^{6}