简体   繁体   English

如何用1-1000的整数创建几乎排序的100数组?

[英]How to create almost sorted array of 100 with integers from 1-1000?

Part of my assignment requires 3 array of input size 100 with integers from 1-1000. 我的作业的一部分需要3个输入大小为100的数组,其整数范围为1-1000。 Where 1st array is of random numbers from 1-1000. 其中第一个数组是1-1000的随机数。 2nd is sorted array from integers 1-1000. 第二个是从整数1-1000排序的数组。 3rd is almost sorted array from integers 1-1000. 第3个几乎是从整数1-1000排序的数组。

My random array.. 我的随机数组

  int array [] = new int [100];    // Random Array of 100 
    for ( int i = 0 ; i < array-1 ; i++) {
      array [i] = (int) (Math.random () * 100);
     }

My sorted array.. 我的排序数组..

  int [] array = new int[100];                    // Sorted Array of 100
    for (int a = 0; a < array.length; a++) {
      array[a] = (a + 1) * 10;
    }

But how to do a almost sorted array. 但是如何做一个几乎排序的数组。 I thought about shuffling but that will just be all random. 我考虑过改组,但这只是随机的。

A faster solution: 更快的解决方案:

int [] array = new int[100];    // Almost sorted Array of 100

array[0] = (int)(Math.random () * 10) + 1;

for (int a = 1; a < array.length; a++) {
  array[a] = array[a-1] + (int)(Math.random() * 12) - 2;
}

Hope that's close enough to an "almost sorted" array. 希望它与“几乎已排序”的数组足够接近。

Try this, 尝试这个,

        int[] values = new int[100];
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < 100; i++)
        {
            values[i] = random.nextInt(900) + 100;
        }

        Arrays.sort(values);
        for (int i = 0; i < 100; i++)
        {
            System.out.println(values[i]);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 1-1000 的整数创建 100 的数组? - How To create Array Of 100 With Integers From 1-1000? 您将如何制作仅包含1-1000(含)值的10000数组? - How would you make an array of 10000 with only values of 1-1000 inclusive? 如何检查整数数组是否已排序? - How to check if an array of integers is sorted? 使用相同类别的两个线程(偶数和奇数)打印1-1000 - Print 1-1000 with two threads (even and odd) from the same class 用另一个数组中的元素替换几乎排序的数组中的元素 - Replacing element in an almost sorted array with an element from another array 如何从奇数和偶数整数数组中仅对奇数整数排序并仅显示排序的奇数整数? - How to sort only odd integers from an array of both odd and even integers and only display the sorted odd integer? 如何在Java数组中存储1亿个整数? - How to store 100 million integers in a Java array? 如何创建包含100个随机整数的一维数组并找到平均值,标准差和方差? - How to create a single dimensional array containing 100 random integers and find the Average value, Standard Deviation and Variance? 如何从要排序的文本文件的内容创建数组? - How to create an array from the contents of a text file to be sorted? 从文本文件中读取整数并将其放入已排序的数组中 - Reading integers from a text file and putting them into a sorted array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM