简体   繁体   中英

array with non repeating numbers from a range in ascending order, java

Im trying to generate an array with 1000 integers of non-repeating numbers in ascending order from 0 to 10,000

So far what I have is:

 public static void InitArray(int[] arr) { // InitArray method
     int i, a_num; // int declared
     Random my_rand_obj = new Random(); // random numbers

     for (i = 0; i <= arr.length-1; i++) // for loop
     {
        a_num = my_rand_obj.nextInt(10000); // acquiring random numbers from 0 - 10000
            arr[i] = a_num; // numbers being put into array (previoulsy declared of size 1000)
     }

  }
public static void ShowArray(int[] arr) { // ShowArray method
     int i; // int declared
     for (i = 0; i <= arr.length-1; i++) { // for loop
        System.out.print(arr[i] + " "); // show current array content
     }
     System.out.println(); // empty line
  }
public static void Sort(int[] arr) { // SortArray method
     int i, min, j; // int decalred
     for (i = 0; i < arr.length-1; i++) { // for loop
        min = i; // min is i
        for (j = i + 1; j < arr.length; j++) { // nested for loop
           if (arr[j] < arr[min]) { // if statement
              min = j; // j is the new minimum
           }
        }       
        int swap = arr[min]; // swap "method"
        arr[min] = arr[i];
        arr[i] = swap;
     }  
  }

Is there any way to check the numbers are not repeating? Is there a function besides the random generator that will let me generate numbers without repeating? Thanks for any help

You can declare array of size 10,000 and init the array in away that each cell in the array will holds the value of it's index:

int [] arr= new  int[10000];
for (int i=0 i < arr.length; i++){
     arr[i] = i
}

Now you can shuffle the array using java Collections. and take the first 1000 items from the array and sort then using java sort.

This will do I believe..

HashSet hs = new HashSet();
for(int i=0;i< arr.length;i++)
    hs.add(arr[i]);
List<Integer> integers=new ArrayList<>(hs);
Collections.sort(integers);

A very simple solution is to generate the numbers cleverly. I have a solution. Though it may not have an even distribution , it's as simple as it can get. So, here goes:

public static int[] randomSortedArray (int minLimit, int maxLimit, int size) {
    int range    = (maxLimit - minLimit) / size;
    int[] array = new int[size];
    Random rand = new Random();
    for (int i = 0; i < array.length; i++ ) {
        array[i] = minLimit + rand.nextInt(range) + range * i;
    }
    return array;
}

So, in your case, call the method as:

int randomSortedArray = randomSortedArray(0, 10_000, 1_000);

It's very simple and doesn't require any sorting algorithm. It simply runs a single loop which makes it run in linear time (ie it is of time complexity = O(1) ).

As a result, you get a randomly generated, "pre-sorted" int[] ( int array) in unbelievable time!

Post a comment if you need an explanation of the algorithm (though it's fairly simple).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM