beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java program for bubble sort in Ascending & descending order

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial we are gonna see how to do sorting in ascending & descending order using Bubble sort algorithm.

Bubble sort program for sorting in ascending Order

import java.util.Scanner;

class BubbleSortExample {
  public static void main(String []args) {
    int num, i, j, temp;
    Scanner input = new Scanner(System.in);
 
    System.out.println("Enter the number of integers to sort:");
    num = input.nextInt();
 
    int array[] = new int[num];
 
    System.out.println("Enter " + num + " integers: ");
 
    for (i = 0; i < num; i++) 
      array[i] = input.nextInt();
 
    for (i = 0; i < ( num - 1 ); i++) {
      for (j = 0; j < num - i - 1; j++) {
        if (array[j] > array[j+1]) 
        {
           temp = array[j];
           array[j] = array[j+1];
           array[j+1] = temp;
        }
      }
    }
 
    System.out.println("Sorted list of integers:");
 
    for (i = 0; i < num; i++) 
      System.out.println(array[i]);
  }
}

Output:

Enter the number of integers to sort:
6
Enter 6 integers: 
12
6
78
9
45
08
Sorted list of integers:
6
8
9
12
45
78

Bubble sort program for sorting in descending Order

In order to sort in descending order we just need to change the logic array[j] > array[j+1] to array[j] < array[j+1] in the above program. Complete code as follows:

import java.util.Scanner;

class BubbleSortExample {
  public static void main(String []args) {
    int num, i, j, temp;
    Scanner input = new Scanner(System.in);
 
    System.out.println("Enter the number of integers to sort:");
    num = input.nextInt();
 
    int array[] = new int[num];
 
    System.out.println("Enter " + num + " integers: ");
 
    for (i = 0; i < num; i++) 
      array[i] = input.nextInt();
 
    for (i = 0; i < ( num - 1 ); i++) {
      for (j = 0; j < num - i - 1; j++) {
        if (array[j] < array[j+1]) 
        {
          temp = array[j];
          array[j] = array[j+1];
          array[j+1] = temp;
        }
      }
    }
 
    System.out.println("Sorted list of integers:");
 
    for (i = 0; i < num; i++) 
      System.out.println(array[i]);
  } 
}

Output:

Enter the number of integers to sort:
6
Enter 6 integers: 
89
12
45
9
56
102
Sorted list of integers:
102
89
56
45
12
9

Enjoyed this post? Try these related posts

  1. Java Program to find Sum of Natural Numbers
  2. Java Program to print Even numbers from 1 to n or 1 to 100
  3. Java Program to reverse the Array
  4. Java program to convert decimal to hexadecimal
  5. Java Program to get input from user
  6. Java Program to check if Number is Positive or Negative

Comments

  1. nasty says

    January 7, 2015 at 11:47 PM

    MAY I ASK HOW TO COMBINE THAT TWO PROGRAMS IN ONE PROGRAM FOR EXAMPLE IN 1 PROGRAM THERES A OUTPUT THAT DISPLAY ASCENDING AND DESCENDING ORDER OF THE INTEGER THAT YOU INPUT

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap