| 1. |
Write a Program to input twenty numbers in an array. Arrange these numbers in descending order using theBubble sort technique. |
|
Answer» 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)); } } |
|