Answer to Question #288556 in Java | JSP | JSF for Highest

Question #288556

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.


1
Expert's answer
2022-01-18T17:22:49-0500
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]);

    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog