You are not logged in. Please login at www.codechef.com to post your questions!

×

TURBO SORT

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

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

asked 16 Sep '17, 10:38

mahimagoyen's gravatar image

0★mahimagoyen
1
accept rate: 0%


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. :)

link

answered 16 Sep '17, 10:44

vijju123's gravatar image

4★vijju123 ♦♦
15.2k11860
accept rate: 18%

edited 16 Sep '17, 10:45

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

(16 Sep '17, 10:50) kunnu1202★

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

(16 Sep '17, 13:32) vijju123 ♦♦4★

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

include<iostream>

using namespace std;

int main()

{

int t,s;

cin >> t;

int a[t];

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

{

    cin >> a[i];

}

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

{

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

    {

        if(a[j]>a[i])

        {

            s=a[i];

            a[i]=a[j];

            a[j]=s;

        }

    }

}

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

{

    cout<<a[i]<<endl;

}

return 0;

}

link

answered 16 Sep '17, 10:49

kunnu120's gravatar image

2★kunnu120
5189
accept rate: 5%

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

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, HeapSort, QuickSort

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...

link

answered 16 Sep '17, 10:58

taran_1407's gravatar image

5★taran_1407
3.9k2387
accept rate: 22%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×145
×1

question asked: 16 Sep '17, 10:38

question was seen: 283 times

last updated: 16 Sep '17, 13:32