Your sorting method is plain and simple O(n^2). Insertion sort is O(number of inversions). There are probably many test cases where the inversions is much less than n^2.
Can anyone help me ? I tried all inputs from board for this program and it gets everything right. I don't know what is wrong with it.
heeelp plx
here is my code
Even if you overcome with the wrong answer you code will result in TLE because of n^2 bubble sort so think of some other efficient method for sorting like quick sort or insertion sort in this case [;)]
Anything other than Accepted is irritating,even Presentation Error
hi please help me my problem is good in my computer but it got TLE
here is mycode:
type
st100=array[1..100]of string;
arr100=array[1..100]of integer;
var
test:integer;
counter:integer;
j,i,tedad,len:integer;
strings,strings2:st100;
number:arr100;
function translate_number(st:string):string;
var
i:integer;
javab:string;
begin
javab:='';
for i:=1 to length(st) do
begin
if st='A' then
javab:=javab+'1';
if st='C' then
javab:=javab+'2';
if st='G' then
javab:=javab+'3';
if st='T' then
javab:=javab+'4';
end;
translate_number:=javab;
end;
procedure sort(var st:string);
var
b,i,j:integer;
mystr:string;
temp:char;
error,x,y:integer;
begin
b:=1;
mystr:=st;
while b=1 do
begin
b:=0;
for i:=1 to length(st)-1 do
begin
val(mystr,x,error);
val(mystr[i+1],y,error);
if x>y then
begin
b:=1;
temp:=mystr;
mystr:=mystr[i+1];
mystr[i+1]:=temp;
counter:=counter+1;
end;
end;
end;
st:=mystr;
end;
procedure sort2(var x:arr100;left,right:integer);
var
i,j,item,temp:integer;
tempstr:string;
begin
i:=left;
j:=right;
item:=x[(left+right) div 2];
repeat
while (x<item)and(i<right) do
i:=i+1;
while (item<x[j])and(j>left) do
j:=j-1;
if i<=j then
begin
temp:=x;
x:=x[j];
x[j]:=temp;
tempstr:=strings[i];
strings[i]:=strings[j];
strings[j]:=tempstr;
i:=i+1;
j:=j-1;
end;
until i > j;
if left<j then
sort2(x,left,j);
if i<right then
sort2(x,i,right);
end;
begin
readln(test);
writeln;
for i:=1 to test do
begin
readln(len,tedad);
for j:=1 to tedad do
begin
readln(strings[j]);
strings2[j]:=translate_number(strings[j]);
end;
for j:=1 to tedad do
begin
counter:=0;
sort(strings2[j]);
number[j]:=counter;
end;
sort2(number,1,tedad);
for j:=1 to tedad do
writeln(strings[j]);
end;
end.
if anybody knows any solutions please....
thanks
m2lajoo