turbo sort

I am getting run time error for my code…please help whats wrong…here is my code on the given link
http://www.codechef.com/viewsolution/4224555

There can be a situation where temp array needs to store more than its capacity, so you can’t define its space to be only 100. Take temp to be dynamic and its space to be equal to capacity of array.
Also include <stdlib.h> into your code. Feel free to ask more! :slight_smile:

Simply use sort function in algorithm header.

I made those changes what you said but its still showing run time error…

my new code is http://www.codechef.com/viewsolution/4240015
please check…

You are not checking when testcase_count==0.
[t <= 10^6] it can be 0,if it is 0 then the program should end.
i guess your modified code gives run time error when you give t as 0.
Reply if it still gives RTE

Use counting sort since the range of elements is small (0 <= A[i] < = 10^6). Maintain a array freq[] of size 10^6 where count[ele] stores the frequency of ele in the given array. Then print ele count[ele] times where ele goes from 0 to 10^6 . Hope it helps.

Instead of using any sort, you can follow a very simple approach here as the problem clearly states that numbers are in the range of 1 to 10^6. So take an array of size 10^6+1 and initialize it to 0.
Take the input as x ,take max_value as 0 and do the following

x : input
max_value : 0
increment a[x]
keep updating **max_value** here.

Now when you are finished taking the input just run a iterative loop from 0 to max_value and print it. The loop can look like

for i:0 to max_value
   if a[i]
      while(a[i]--)
           print i

Time complexity : O(n)

Hope it helps!!