Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.

Java Programming Language (Article) (Program)

9

Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.

Program:

import java.util.Scanner;

public class RansariBubbleSort
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = 15;
        int arr[] = new int[n];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        //Bubble Sort
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t;
                }
            }
        }

        System.out.println("Sorted Array:");
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output:

Enter the elements of the array:
23 1 2 3 54 67 4 5 6 7 8 9 34 67 97
Sorted Array:
1 2 3 4 5 6 7 8 9 23 34 54 67 67 97 Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.