Bubble Sort(Descending) in Java

Descending bubble sorting is a variation of the bubble sort algorithm where elements are sorted in descending order. Similar to ascending bubble sorting, it iterates through the list of elements, comparing adjacent pairs and swapping them if they are in the wrong order. However, in descending bubble sorting, the largest element 'bubbles up' to the beginning of the list with each iteration, resulting in the list being sorted in descending order, with the largest element at the beginning and the smallest element at the end.

JAVA PROGRAMMING

Raki Adhikary

1/12/20231 min read

import java.util.*;

public class BubbleSortDescending {

public static void main(String args[]) {

int size;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the size of the array:");

size = scanner.nextInt();

int array[] = new int[size];

int i, j, temp;

/* Taking input in the array */

for (i = 0; i < size; i++) {

System.out.println("Enter element at index " + i + ":");

array[i] = scanner.nextInt();

}

/* Bubble Sorting the array in descending order */

for (i = 0; i < size - 1; i++) {

for (j = 0; j < size - 1 - i; j++) {

if (array[j] < array[j + 1]) {

// Swapping if the current element is less than the next one

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

}

}

/* Displaying the sorted Array */

System.out.println("The sorted Array in descending order is:");

for (i = 0; i < size; i++)

System.out.print(array[i] + " ");

scanner.close();

}

}

Your Code is here