Interview Question
Country: United States
Have one more pass to store the number as they were in original array. This would be something like this.
if(ceil(a[i])==floor(a[i]))
a[i]= int(a[i]);
This won't work if there are numbers like 2.0 , 3.0 in the original array.
You can do one more thing. Sort the number by any method . For all comparison do something like this
if((float)a >float(b)
..
Make all other operations (swapping) with original values
In java we can use java.lang.Number which is parent class of Integer and Float
{{
Number array[]={1,1.2f,4.4f,4};
}}
Java 1.5 or higher version will support auto boxing so no need to do any thing
If i want to store them in sorted order i would use TreeSet<Number, myComparator>()
myComparator would implement Comparator<Number>
If i want to sort unsorted list, i would go for Collections.sort(list, myCompartator);
// Type your Java code and click the "Run Code" button!
// Your code output will be shown on the left.
// Click on the "Show input" button to enter input data to be read (from
stdin).
public class Interpreter {
public static void main(String[] args) {
// Start typing your code here...
System.out.println("Hello world!");
double[] arr={1,2,3.98,-2,6.545};
findsort(arr);
}
public static String[] findsort(double[] arr)
{
String[] temp=new String[arr.length];
for(int i=0;i<arr.length;i++)
{
temp[i]=new Double(arr[i]).toString();
}
java.util.Arrays.sort(temp);
for(String x:temp){
System.out.println(x);
}
return arr;
}
}
public static void main(String[] args) {
List<Number> list=new ArrayList<Number>(0);
list.add(10);
list.add(10.20);
list.add(9.5);
System.out.println(list);
Collections.sort(list,new Comp());
System.out.println(list);
}
static class Comp implements Comparator< Number>{
public int compare(Number o1, Number o2) {
return ((Float)Float.parseFloat(o1.toString())).compareTo(Float.parseFloat(o2.toString()));
}
}
try this.
public class TestCollection
{
public static void main(String args[])
{
ArrayList<Number> list = new ArrayList<Number>();
list.add(10);
list.add(20);
list.add(1.5f);
list.add(3);
Collections.sort(list,new xyz());
Iterator<Number> iterator = list.listIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
class xyz implements Comparator<Number>
{
public int compare(Number arg0, Number arg1)
{
BigDecimal ar0 = new BigDecimal(arg0.doubleValue());
BigDecimal ar1 = new BigDecimal(arg1.doubleValue());
return ar0.compareTo(ar1);
}
}
Use a comparator:
import java.util.*;
class D implements Comparator<Number>
{
@Override
public int compare(Number arg0, Number arg1) {
float i = (arg0.floatValue() - arg1.floatValue()) ;
if(i==0)
{
return 0;
}
else
{
if (i>0) {return 1;}
else{
if (i< 0){return -1;}
}
}
return 0;
}
}
public class Test1 {
public static void main(String[] args)//generates the exception
{
List s1 = new LinkedList();
s1.add(5);
s1.add(2);
s1.add(3.2);
s1.add(4.6);
Collections.sort(s1,new D());
System.out.println(s1);
}
}
You can sort all of them as float numbers.
- V.Krestyannikov July 27, 2012