Java 合并对数字数组进行排序

Java 合并对数字数组进行排序,java,Java,我已经创建了一个程序来对用户输入的元素的短字符串进行合并排序。我想用随机生成的数字填充Begging数组。通过使用math.random可以做到这一点吗?另外,如何在给定范围内生成数字?(即5-50或0-1)。谢谢你的帮助,我已经在下面包含了我的代码 public class MergeSort { public static void main(String[] args) { //for (int i = 0; i < 10000; ++i)

我已经创建了一个程序来对用户输入的元素的短字符串进行合并排序。我想用随机生成的数字填充Begging数组。通过使用math.random可以做到这一点吗?另外,如何在给定范围内生成数字?(即5-50或0-1)。谢谢你的帮助,我已经在下面包含了我的代码

public class MergeSort 
{
    public static void main(String[] args) 
    {
        //for (int i = 0; i < 10000; ++i)
        //{
        //  String[i] = Math.random();
        //}
        //Unsorted array
        Integer[] a = { 2, 6, 3, 5, 1, 4, 10};

        //Call merge sort
        mergeSort(a);

        //Check the output which is sorted array
        System.out.println(Arrays.toString(a));
    }

    public static Comparable[] mergeSort(Comparable[] list) 
    {
        //If list is empty; no need to do anything
        if (list.length <= 1) {
            return list;
        }

        //Split the array in half in two parts
        Comparable[] first = new Comparable[list.length / 2];
        Comparable[] second = new Comparable[list.length - first.length];
        System.arraycopy(list, 0, first, 0, first.length);
        System.arraycopy(list, first.length, second, 0, second.length);

        //Sort each half recursively
        mergeSort(first);
        mergeSort(second);

        //Merge both halves together, overwriting to original array
        merge(first, second, list);
        return list;
    }


    private static void merge(Comparable[] first, Comparable[] second, Comparable[] result) 
    {
        //Index Position in first array - starting with first element
        int iFirst = 0;

        //Index Position in second array - starting with first element
        int iSecond = 0;

        //Index Position in merged array - starting with first position
        int iMerged = 0;

        //Compare elements at iFirst and iSecond, 
        //and move smaller element at iMerged
        while (iFirst < first.length && iSecond < second.length) 
        {
            if (first[iFirst].compareTo(second[iSecond]) < 0) 
            {
                result[iMerged] = first[iFirst];
                iFirst++;
            } 
            else
            {
                result[iMerged] = second[iSecond];
                iSecond++;
            }
            iMerged++;
        }
        //copy remaining elements from both halves - each half will have already sorted elements
        System.arraycopy(first, iFirst, result, iMerged, first.length - iFirst);
        System.arraycopy(second, iSecond, result, iMerged, second.length - iSecond);
    }
}
公共类合并排序
{
公共静态void main(字符串[]args)
{
//对于(int i=0;i<10000;++i)
//{
//String[i]=Math.random();
//}
//未排序数组
整数[]a={2,6,3,5,1,4,10};
//调用合并排序
合并(a);
//检查排序为数组的输出
System.out.println(Arrays.toString(a));
}
公共静态可比[]合并排序(可比[]列表)
{
//如果列表为空,则无需执行任何操作

if(list.length以下是如何构造一个充满随机整数的数组。(在本例中,它们是大于或等于0且小于100的随机整数。)


下面是如何构造一个充满随机整数的数组。(在本例中,它们是大于或等于0且小于100的随机整数。)


请阅读并避免发表“空话”。提出一个单一、连贯、具体的问题。不要使用编码器输入值,我想要一个随机生成的数字列表,我可以控制数字的数量和范围。@Amit.Im假设math.random在这种情况下会有所帮助。@tomlangdor这样更好。你应该编辑你的问题来提问。(删除所有与“合并排序”相关的内容,因为这与您的问题无关。)请阅读并避免发布“废话”。问一个单一的、连贯的、具体的问题。不要使用编码器的输入值,我想要一个随机生成的数字列表,我可以控制数字的数量和范围。@Amit.Im假设math.random在这种情况下会有帮助。@tomlangdor更好。你应该编辑你的问题,只问这个问题。(删除与此相关的所有内容。)“合并排序”,因为这与您的问题无关。)谢谢您的帮助。我如何在我的代码中实现这一点?@Tomlangdorr我不知道如何回答。我想,复制/粘贴到您的代码中?我如何实现双精度值并用当前字符串替换此字符串?获取双精度:
rand.nextDouble()*(max-min)+min
我不知道如何“用我当前的字符串替换此字符串”“意思是。谢谢你的帮助。我该如何在我的代码中实现它?@Tomlangdorr我不知道该如何回答。我想,复制/粘贴到你的代码中吧?我该如何实现双精度值并用当前字符串替换这个字符串?要获得双精度值:
rand.nextDouble()*(max-min)+min
我不知道是什么“用我的当前字符串替换此字符串”表示。
int[] a = new int[10];

Random rand = new Random();

for (int i = 0; i < 10; i++) {
    a[i] = rand.nextInt(100);
}
rand.nextInt(max-min) + min