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
read(t);
for x := 1 to t do
begin
readln(N);
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
please tell on which line i have array out of bound?
This is not the right approach to sort 10^6 numbers.
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
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.
This is correct code
program TSORT;
var
t,x,N,y,temp:longint;
num:array[1..100000] of longint;
begin
read(t);
for x := 1 to t do
begin
read(N);
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.
I have corrected your code.
see below.
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]?
but it wont fetch you ac…
yup…use read()…
and 1000000…
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?
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[1000000] array size