Answer to Question #286522 in Java | JSP | JSF for Alexander

Question #286522

Write a program to store 30 numbers in an array. Then display those numbers in descending order using bubble sorting with a condition that maximum 5 numbers will appear in one line.

1
Expert's answer
2022-01-11T11:06:37-0500


import java.util.Scanner;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);


		int numbers[] = new int[30];
		for (int i = 0; i < 30; i++) {
			System.out.print("Enter the number " + (i + 1) + ": ");
			numbers[i] = keyBoard.nextInt();
		}
		bubbleSort(numbers);


		System.out.println("\nAll numbers in descending order");
		for (int i = 0; i < 30; i++) {
			System.out.print(numbers[i] + " ");
			if ((i+1) % 5 == 0) {
				System.out.print("\n");
			}
		}


		keyBoard.close();
	}


	static void bubbleSort(int numbers[]) {
		int n = numbers.length;
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < n - i - 1; j++)
				if (numbers[j] < numbers[j + 1]) {
					int temp = numbers[j];
					numbers[j] = numbers[j + 1];
					numbers[j + 1] = temp;
				}
	}
}

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