Java program for ascending order and descending order using random class
Answers
Answered by
0
import java.util.Scanner; class BubbleSortExample { public static void main(String []args) { int num, i, j, temp; Scanner input = new Scanner(System.in); System.out.println("Enter the number of integers to sort:"); num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter " + num + " integers: "); for (i = 0; i < num; i++) array[i] = input.nextInt(); for (i = 0; i < ( num - 1 ); i++) { for (j = 0; j < num - i - 1; j++) { if (array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } System.out.println("Sorted list of integers:"); for (i = 0; i < num; i++) System.out.println(array[i]); } }
Output:
Enter the number of integers to sort: 6 Enter 6 integers: 12 6 78 9 45 08 Sorted list of integers: 6 8 9 12 45 78
Bubble sort program for sorting in descending Order
In order to sort in descending order we just need to change the logic array[j] > array[j+1] to array[j] < array[j+1] in the above program. Complete code as follows:
import java.util.Scanner; class BubbleSortExample { public static void main(String []args) { int num, i, j, temp; Scanner input = new Scanner(System.in); System.out.println("Enter the number of integers to sort:"); num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter " + num + " integers: "); for (i = 0; i < num; i++) array[i] = input.nextInt(); for (i = 0; i < ( num - 1 ); i++) { for (j = 0; j < num - i - 1; j++) { if (array[j] < array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } System.out.println("Sorted list of integers:"); for (i = 0; i < num; i++) System.out.println(array[i]); } }
Output
Enter the number of integers to sort: 6 Enter 6 integers: 89 12 45 9 56 102 Sorted list of integers: 102 89 56 45 12 9
Output:
Enter the number of integers to sort: 6 Enter 6 integers: 12 6 78 9 45 08 Sorted list of integers: 6 8 9 12 45 78
Bubble sort program for sorting in descending Order
In order to sort in descending order we just need to change the logic array[j] > array[j+1] to array[j] < array[j+1] in the above program. Complete code as follows:
import java.util.Scanner; class BubbleSortExample { public static void main(String []args) { int num, i, j, temp; Scanner input = new Scanner(System.in); System.out.println("Enter the number of integers to sort:"); num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter " + num + " integers: "); for (i = 0; i < num; i++) array[i] = input.nextInt(); for (i = 0; i < ( num - 1 ); i++) { for (j = 0; j < num - i - 1; j++) { if (array[j] < array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } System.out.println("Sorted list of integers:"); for (i = 0; i < num; i++) System.out.println(array[i]); } }
Output
Enter the number of integers to sort: 6 Enter 6 integers: 89 12 45 9 56 102 Sorted list of integers: 102 89 56 45 12 9
AnuragPatel:
Thanks brother
Answered by
0
Explanation:
plz follow Sudarshana Ganguly for best answers to ur questions..
pls follow her , she is in my following list she will give you accurate answer
Similar questions