| 1. |
Write a Program to input twenty numbers in an array. Arrange these numbers in descending order using the Bubble sort technique. |
|
Answer» ong>import java.util.Arrays; import java.util.Scanner; public class Sorting { static void decendingBubbleSort(int[ ] array) { BOOLEAN isSwapped; for (int i = 0; i < array.length - 1 ; i++) { isSwapped = FALSE; for (int j = 0; j < array.length - i - 1 ; j++) if (array[j] < array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; isSwapped = true; } if (!isSwapped) break; } } public static void main(String[ ] args) { int[ ] array = new int[20]; System.out.println("Enter 20 NUMBERS - "); for (int i = 0; i < array.length; i++) array[i] = new Scanner(System.in).nextInt( ); decendingBubbleSort(array); System.out.println("Sorted the Array in Decending order - " + Arrays.toString(array)); } } |
|