turbo sort in c

#include<stdio.h>
int main()
{
int t,a[10^6];
int i,j,temp;
printf("\n enter the no. of test cases");
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("\n%d",&a[i]);
}
for(i=0;i<t;i++)
{
for(j=i+1;j<t;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<t;i++)
{
printf("\n%d",a[i]);
}
}

hii. this is my second program in codechef. am a begineer. can anyone help me why am getting a run time error?? plz explain ur answer. it would be a great help for me to get more interest in programming… thanks in advance…:slight_smile:

you have to return 0 at the end of main()

Hello maha94,

I also suspect that after you return 0 and run your program again you will get WA…

The main characteristic of sites like Codechef where the programs are tested by an online judge is that you must follow strictly the format of the input cases and must ensure that your output is also formatted according to the particular problem you’re solving…

So, the line:

 printf("\n enter the no. of test cases");

must be removed from your code if you want to get the Accepted result.

Also, on a side note, it seems to me that you’re using a quadratic sorting algorithm… Like Bubblesort or some variant of it, right?

That will also for sure give you a non-accepted status result, much likely, you’ll see Time Limit Exceeded…

Remember that Codechef is fundamentally an algorithms website and not a place to train implementation and/or programming in general (I’d say those skills must be already developed up to a point before you can tackle algorithmic problems efficiently…).

To help you out, I suggest you read C language documentation regarding built-in sorting functions…

Here is the link:

Sorting in C.

I wish you the best of luck in your programming quest,

Bruno

2 Likes

Thanks! ,

@kuruma Please tell if qsort uses QuickSort Algorithm in one way or with some implements… or some other…

Beacause had it been using quicksort One would also be not able to to get AC…instead an TLE.(As I got First).

Also Can’t we use Merge or Heap…as they are nlogn…Or just you said it for STL Advantages over coding blindlessly.

As far as I know, C library qsort implements quicksort on most cases, and as far as I also know, you should be able to get AC with it in principle… As I used C++ built-in function sort_heap whose average running time is the same as quicksort…

I also think that there are several other built-in C sorting functions… But I’m not sure how to use them… I will attempt to submit a solution to this problem coded in C and I will let you know how it goes…

Bruno

you have to return 0 at the end of main()

What is the problem men when i am writing the counting sort in Java its giving TLE error but same code when i wrote in C i got all Cleared why this is so??

First of all there is nothing wrong with your code which can lead to segmentation fault (or a run time error, like you said).

If you are using an Old Compiler which really rely on this, well happens only because that compiler is using an earlier version then C99.

Starting with C99 there is no need of return type of main as return 0, because is already implicit, this means that the following program is corect an Legal:

#include<stdio.h>

int main(void){
    
    /* code here */
    /* no return needed */
}

Another thing, starting with C11 you can use VLA (Variable Length Array) so this mean that the following program is Legal and you can take the benefit of creating an Unknown fixed Array SIZE (Unknown as long, as you don’t consume the whole STACK of course):

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int size,temp;

    printf("Please type the SIZE of the Array: ");
    if( (scanf("%d",&size)) != 1){
        printf("Error scanf, Fix it!");
        exit(1);
    }

    int array[size];

    printf("Please type those %d Elements of the Array: ",size);
    for (int i=0 ; i<size ; i++){
        if ( (scanf("%d",&array[i])) != 1){
            printf("Error scanf, Fix it!");
            exit(2);
        }
    }

    for (int i=0 ; i<size ; i++){
        for (int j=i+1 ; j<size ; j++){
            if (array[i] > array[j]){
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    printf("The sorted Array is: ");
    for(int i=0 ; i<size ; i++){
        printf("%d ",array[i]);
    }
    
    printf("\n");
}

Output:

Please type the SIZE of the Array: 5
Please type those 5 Elements of the Array: 9 3 4 8 2
The sorted Array is: 2 3 4 8 9

You probably noticed that I checked the return of SCANF and if fails the program stops. Many programmers ignores this important part. Your program should not continue if there is an SCANF error.

Another benefit (starting with C99) is declaring the int i direct inside of the for loop like this:

for (int i=0 ; i<size ; i++){}

But of course avoid declaring the same i in nested for loops :)…like here:

    for (int i=0 ; i<size ; i++){
        for (int i ; i<size ; i++){
            /* code here */ 
        }
    }

Read more about C11 standard and if possible use it.