vendredi 15 novembre 2019

Generating random array of fixed length (Java)

im just looking to change my code so that a random array of a fixed length of 100 integers is generated every time the code is ran rather than just have a pre-set array within the code. Im quite new to this so just need a guide in the right direction, thanks

    public class Selectionsort { 

  public static void main(String[] args) { 

      int[]numbers= {15,8,6,21,3,54,6,876,56,12,1,4,9}; 

      sort(numbers); 

      printArray(numbers); 

  } 



  public static int[] sort(int[] A){ 

      for(int i=0; i< A.length-1; i++){ 

          int minIndex = i; 

          for(int j = i + 1; j < A.length; j++){ 

              if(A[j] < A[minIndex]){ 

                  minIndex = j; 

              } 

          } 

          int temp = A[minIndex]; 

          A[minIndex] = A[i]; 

          A[i] = temp; 

      } 



      return A; 

  } 



  public static void printArray(int[] A){ 

      for(int i=0; i<A.length; i++){ 

          System.out.println(A[i]); 

      } 

  } 

   } 



Aucun commentaire:

Enregistrer un commentaire