java program bubble sort in ascending and descending order

Solutions on MaxInterview for java program bubble sort in ascending and descending order by the best coders in the world

showing results for - "java program bubble sort in ascending and descending order"
Mae
29 Mar 2017
1// Bubble sort descending order
2import java.util.Scanner;
3public class BubbleSortDescending 
4{
5   public static void main(String[] args) 
6   {
7      int number, a, b, temp;
8      Scanner sc = new Scanner(System.in);
9      System.out.println("Please enter number of integers: ");
10      number = sc.nextInt();
11      int[] arrInput = new int[number];
12      System.out.println("Enter " + number + " integers: ");
13      for(a = 0; a < number; a++) 
14         arrInput[a] = sc.nextInt();
15      for(a = 0; a < (number - 1); a++) 
16      {
17         for(b = 0; b < number - a - 1; b++) 
18         {
19            // logic to sort in descending order
20            if(arrInput[b] < arrInput[b + 1]) 
21            {
22               temp = arrInput[b];
23               arrInput[b] = arrInput[b + 1];
24               arrInput[b + 1] = temp;
25            }
26         }
27      }
28      sc.close();
29      System.out.println("Sorted integers: ");
30      for(a = 0; a < number; a++) 
31         System.out.println(arrInput[a]);
32   }
33}