Posted by: khai_k_khai_k April 29, 2008
Java Program Help Plz
Login in to Rate this Post:     0       ?        
hi DevilWithin999,
I got a linear time (O(n)) solution but it might take a lot of space in worst case.

public class Sort {

    public static void main(String[] args) {
        int[] inputArray = new int[]{1, 4, 5, 4, 4, 1};
       
        //Calculate the maximum length required for bucket array 
        int max = inputArray.length;
        for(int i:inputArray){
            if(i>max){
                max = i;
            }
        }
       
        int bucketArray[] = new int[max];
        for(int i =0;i<inputArray.length;i++){
            bucketArray[inputArray[i]]++;
        }
       
        for(int i = 0;i<bucketArray.length;i++){
            if(bucketArray[i]!=0){
                System.out.println("Number : " + i + " Count: " + bucketArray[i]);
            }
        }

    }
}


Happy Programming.
Read Full Discussion Thread for this article