0

I have created an implementation of radixsort using ArrayLists of integer arrays. The code is functional however as input size increases passed about 100,000 execution time is far too high. I need this code to be able to handle an input of 1,000,000 integers. What can I do to optimize execution time?

public class RadixSort {

    public static void main(String[] args) {

        // generate and store 1,000,000 random numbers
        int[] nums = generateNums(1000000);

        // sort the random numbers
        int[] sortedNums = radixSort(nums);

        // check that the array was correctly sorted and print result
        boolean sorted = isSorted(sortedNums);
        if (sorted) {
            System.out.println("Integer list is sorted");
        } else {
            System.out.println("Integer list is NOT sorted");
        }
    }

    /**
     * This method generates numCount random numbers and stores them in an integer
     * array. Note: For our program, numCount will always equal 1,000,000.
     * 
     * @return nums the new integer array
     */
    public static int[] generateNums(int numCount) {

        // create integer array with length specified by the user
        int[] nums = new int[numCount];
        int max = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (int) (Math.random() * max + 1);
        }

        // return new integer array
        return nums;
    }

    /**
     * This method implements a radix sort
     * 
     * @param nums
     *            the integer array to sort
     * @return nums the sorted integer array
     */
    public static int[] radixSort(int[] nums) {

        // find max number of digits in an element in the array
        int maxDigits = findMaxDigits(nums);

        // specified decimal place
        int digit = 1;

        // create 10 buckets
        ArrayList<Integer>[] buckets = new ArrayList[10];

        // iterate through the list for as many times as necessary (maxDigits)
        for (int j = 0; j < maxDigits; j++) {

            // initialize buckets as ArrayLists
            for (int i = 0; i < buckets.length; i++) {
                buckets[i] = new ArrayList<Integer>();
            }

            // isolate digit at specified decimal place and add the number to the
            // appropriate bucket
            for (int i = 0; i < nums.length; i++) {
                int last = (nums[i] / digit) % 10;
                buckets[last].add(nums[i]);

                // convert buckets to an integer array
                nums = convertToIntArray(buckets, nums);
            }

            // update digit to find next decimal place
            digit *= 10;
        }

        // return sorted integer array
        return nums;
    }

    /**
     * This method converts from an ArrayList of integer arrays to an integer array
     * 
     * @param buckets
     *            the ArrayList of integer arrays
     * @param nums
     *            the integer array to return
     * @return nums the new integer array
     */
    public static int[] convertToIntArray(ArrayList<Integer>[] buckets, int[] nums) {

        // loop through the ArrayList and put elements in order into an integer array
        int i = 0;
        for (ArrayList<Integer> array : buckets) {
            if (array != null) {
                for (Integer num : array) {
                    nums[i] = num;
                    i++;
                }
            }
        }

        // return new integer array
        return nums;
    }

    /**
     * Method to find the max number of digits in a number in an integer array
     * 
     * @param nums
     *            the integer array to search through
     * @return maxLength the max number of digits in a number in the integer array
     */
    public static int findMaxDigits(int[] nums) {

        // find maximum number
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            }
        }

        // find and return the number of digits in the maximum number
        int maxLength = String.valueOf(max).length();
        return maxLength;
    }

    /**
     * This method is used for testing correct functionality of the above radixSort
     * method.
     * 
     * @param nums
     *            the integer array to check for correct sorting
     * @return false if the array is sorted incorrectly, else return true.
     */
    public static boolean isSorted(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                return false;
            }
        }
        return true;
    }
}
vinS
  • 1,417
  • 5
  • 24
  • 37
  • 2
    100,000 or 1,000,000? How long is is too long? – shmosel Dec 27 '17 at 03:04
  • 100,000 is too long and I need it to handle up to 1,000,000 – Anna Colman Dec 27 '17 at 03:12
  • have you read [this](https://stackoverflow.com/questions/43701076/radix-sort-optimization) and [this](https://github.com/iwiwi/parallel-radix-sort) and [this](https://codereview.stackexchange.com/questions/129030/radix-sort-in-java) – Polish Dec 27 '17 at 03:45

0 Answers0