InterviewSolution
| 1. |
write a java program to create an integer array of 20 elements. Sort the array using bubble sort technique and display the sorted array. |
|
Answer» Java program for bubble sort in Ascending & descending orderBY DEEPTHIKA TALLURI | FILED UNDER: JAVA EXAMPLESIn this tutorial we are gonna see how to do sorting in ascending & descending order using Bubble sort algorithm.Bubble sort program for sorting in ascending Orderimport 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:6Enter 6 integers: 1267894508Sorted list of integers:689124578 |
|