Home Java Programming Language / Programs / Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort technique.
🚀 Programming Example

Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort technique.

👁 164 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

💻 Program Code

import java.util.Scanner;

public class RAnsariBubbleSortDsc
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = 10;
        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] + " ");
        }
    }
}
                        

🖥 Program Output

Enter the elements of the array:
15 7 9 54 78 21 45 76 1 43
Sorted Array:
78 76 54 45 43 21 15 9 7 1 Press any key to continue . . .


                            
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.