01. Example:
import java.util.Scanner; class bubbleSort { public static void main(String[] args) { int n, c, d, swap; Scanner in = new Scanner(System.in); System.out.println("Input number of integers to sort: "); n = in.nextInt(); int array[] = new int[n]; System.out.println("Enter " + n + " integers: "); for (c = 0; c < n; c++){ array[c] = in.nextInt(); } for (c = 0; c < (n - 1); c++) { for (d = 0; d < n - c - 1; d++) { if (array[d] > array[d + 1]){ swap = array[d]; array[d] = array[d + 1]; array[d + 1] = swap; } } } System.out.println("Sorted List: "); for (c = 0; c < n; c++){ System.out.println(array[c]); } } }
Output:
Input number of integers to sort: 11 Enter 11 integers: 89 90 34 45 17 54 67 23 65 35 78 Sorted List: 17 23 34 35 45 54 65 67 78 89 90
The java code implements a bubble sort algorithm to sort an array of integers in ascending order. The program takes input from the user, the number of integers to sort and the integers themselves. It then sorts the array using the bubble sort algorithm and prints out the sorted list.