试图减少Radix排序程序执行时间(Java)

时间:2017-12-27 03:04:00

标签: java radix-sort

我使用整数数组的ArrayLists创建了radixsort的实现。代码是有用的,但随着输入大小的增加,大约100,000次执行时间太长。我需要这段代码才能处理1,000,000个整数的输入。我该怎么做才能优化执行时间?

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;
    }
}

0 个答案:

没有答案