Quick logic question really, I have a program which generates a random list of numbers, and is supposed to insert the numbers into the list in order. Here''s what I''ve written so far, i think my logic my be flawed though due to scope.
// insert input_item to an ordered list, the list must be still in
// correct order after the insertion. return the size of list.
// TASK 1 -- add you code here
public int insertInOrder(String input_item)
{
int i,j,insertPos;
if(current_size>0)
{
int conversion = Integer.parseInt(input_item);
for(i=current_size;i>conversion;i--)
tmp[i+1]=data[i]; //temporary buffer
for(j=0;j<=current_size+1;j++)
{
if(input_item.equals(data[j]) | Func.lessThan(input_item,data[j]))
data[j]=input_item;
}
current_size++;
}
else
{
data[0] = input_item;
current_size++;
}
return current_size;
}
Oh and here''s how Func.lessThan is defined
public static boolean lessThan(String s1, String s2)
// compare two strings s1 and s2 return true if s1 < s2
{
if(isIntString(s1) && isIntString(s2))
return (Integer.parseInt(s1) < Integer.parseInt(s2));
else
return (s1.compareTo(s2) < 0);
}