Write a program to create an array in order to store 30 numbers then display the highest number and second highest number present in it using bubble sorting.
package com.task;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[] arr = new int[30];
for (int i = 0; i < arr.length; i++) {
arr[i] = new Random().nextInt();
}
int n = arr.length;
int temp = 0;
for(int i = 0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] < arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Comments
Leave a comment