A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to sort an array in descending order using bubble sort
Q:

Java program to sort an array in descending order using bubble sort

0

Java program to sort an array in descending order using bubble sort

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create an array of integers then we will sort array elements in descending order using bubble sort.

In the bubble sort sorting technique, we compare adjacent elements and swap if they are in the wrong order. This process repeats until no more swaps are needed.

Program/Source Code:

The source code to sort an array in descending order using bubble sort is given below. The given program is compiled and executed successfully.

// Java program to sort an array in descending order 
// using bubble sort

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int i = 0;
    int j = 0;
    int t = 0;

    int arr[] = {14, 49, 79, 87, 78};

    //Sort array using bubble sort.
    while (i < 5) {
      j = 4;
      while (j > i) {
        if (arr[j] > arr[j - 1]) {
          t = arr[j];
          arr[j] = arr[j - 1];
          arr[j - 1] = t;
        }
        j = j - 1;
      }
      i = i + 1;
    }

    System.out.println("Sorted Array in descending order: ");
    i = 0;
    while (i < 5) {
      System.out.print(arr[i] + " ");
      i = i + 1;
    }
  }
}

Output:

Sorted Array in descending order: 
87 79 78 49 14

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now