# Turbo Sort encountered runtime error.

hey can anyone find the bug in this program problem link

``````
program TSORT;

var
t,x,N,y,temp:longint;
num:array[1..100000] of longint;
begin

for x := 1 to t do
begin
num[x] := N;
end;

for x := 1 to t do
begin
for y := 1 to t do
begin
if num[y] > num[y+1] then
begin
temp := num[y];
num[y] := num[y+1];
num[y+1] := temp;
end;

end;
end;

for x := 2 to t+1 do
begin
writeln(num[x]);
end;
end.

``````

i run it on codechef code,compile & run it ran there succefully with giving right results but when i submitted it encountered an runtime error.

@vinay1729
Array out of bound.

Althogh this aproach wont fetch you Correct answer.
You need a better approach.

1 Like

This is not the right approach to sort 10^6 numbers.

Use counting sort.

i am not quite familiar with pascal.
But by seeing…

First of all…the two nested loop are wrong.
it should be…

``````for x := 1 to t do

begin

for y := x+1 to t do

begin``````

This is correct code

``````program TSORT;

var
t,x,N,y,temp:longint;
num:array[1..100000] of longint;
begin

for x := 1 to t do
begin
num[x] := N;
end;

for x := 1 to t do
begin
for y := x+1 to t do
begin
if num[x] > num[y] then
begin
temp := num[x];
num[x] := num[y];
num[y] := temp;
end;

end;
end;

for x := 1 to t do
begin
writeln(num[x]);
end;
``````

end.

1 Like

Yes…Its not efficient u can use counting sort.
here is a method.
Just count how many u input 1,2 3,…
and print.

take an array of size 10^6.

initialize it with 0.

now when you input a number x.
then you should do a[x]=a[x]+1.

suppose you input 1 1 2 2 2 4 5 5.
then a becomes(1-indexing) 2 3 0 1 2 0 0 0 0…

it means 1 is occuring 2 times 2 is occuring 3 times…

How to declare and array of 10^6 size,since the above code is not working on arr array size

please tell on which line i have array out of bound?

use counting sort.

if num[x] > num[y] then
begin
temp := num[x];
num[x] := num[y];
num[y] := temp;
end;

is this only the bug?, if there is more please do tell.

see below.

Hey but it not worked.

hey is the problem is caused because of read() so rather i should use readln() also i have made array of size 1^100000 only however problem says [t <= 10^6] & [0 <= N <= 10^6]?

and 1000000…

but it wont fetch you ac…

will it fetch me AC it not fetched me AC rather it says now TLE so i think solution is ok but algorithms is not efficiently fast right?

hi, so I’ve used a same logic but it keeps giving me a runtime error.

``````#include <stdio.h>
int main(void)
{
long int t,i,j,temp;
long int arr;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<t-1;i++)
{
for(j=0;j<t-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(i=0;i<t;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}``````