iNxmi

Min Number Algorithm (Bubble Sort) - Java

Nov 4th, 2022 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | Source Code | 0 0
  1. package com.nami;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6.  
  7.     public Main() {
  8.         //empty array with a length of 3
  9.         int[] list = new int[3];
  10.         //biggest possible number
  11.         int maxNum = 100;
  12.  
  13.         //assigning random values between 0 and MAXNUM to the array
  14.         for (int i = 0; i < list.length; i++)
  15.             list[i] = (int) (Math.random() * maxNum);
  16.         //printing the random array (LIST)
  17.         System.out.println(Arrays.toString(list));
  18.  
  19.         //setting n to the first value of LIST
  20.         int n = list[0];
  21.         //looping through every element of LIST starting with the 2nd value
  22.         for (int i = 1; i < list.length; i++)
  23.             //if n is bigger than value from LIST with index I then set N to the value
  24.             if (n > list[i])
  25.                 n = list[i];
  26.  
  27.         //print N (the smallest number)
  28.         System.out.println(n);
  29.     }
  30.  
  31.     public static void main(String[] args) {
  32.         new Main();
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment