Udemy coupon 100 off 2017 ::Udemy coupon code free :: Get 100+ Free Udemy coupons Here

Bubble Sort in ascending and descending order

Bubble Sort in ascending order

Out Put is as following :



Source code: 

import java.io.*;
public class BubbleSort
{
public static void main(String[] args) throws IOException
{
int i,j,t;
int [] array_values=new int[50];
  
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter the Total Number Of  Elements You Want to Enter : ");
  int n=Integer.parseInt(br.readLine());

   System.out.println("Now enter the values to Short  : ");
  for(i=0;i<n;i++)
{
         array_values[i]=Integer.parseInt(br.readLine());
     }

  for(i = 0; i < n; i++)
{
  for(j = 1; j < (n-i); j++)
{
 if(array_values[j-1] > array_values[j])
{
   t = array_values[j-1];
   array_values[j-1]=array_values[j];
  array_values[j]=t;
   }
  }
 }
System.out.println("\n\nShorted Values are : ");
  for(i = 0; i < n; i++)
{
System.out.println(array_values[i]);
}
}
}

  • Save the file with name BubbleSort.java 
  • Complie with: javac BubbleSort.java
  • Run with: java BubbleSort


Bubble Sort in descending order

Out Put is as following :

Source code: 

import java.io.*;
public class BubbleSortDesc
{
public static void main(String[] args) throws IOException
{
int i,j,t;
int [] array_values=new int[50];
  
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter the Total Number Of  Elements You Want to Enter : ");
  int n=Integer.parseInt(br.readLine());

   System.out.println("Now enter the values to Short  : ");
  for(i=0;i<n;i++)
{
         array_values[i]=Integer.parseInt(br.readLine());
     }

  for(i = 0; i < n; i++)
{
  for(j = 1; j < (n-i); j++)
{
 if(array_values[j-1] < array_values[j])
{
   t = array_values[j-1];
   array_values[j-1]=array_values[j];
  array_values[j]=t;
   }
  }
 }
System.out.println("\n\nShorted Values are : ");
  for(i = 0; i < n; i++)
{
System.out.println(array_values[i]);
}
}
}

  • Save the file with name BubbleSortDesc.java 
  • Complie with: javac BubbleSortDesc.java
  • Run with: java BubbleSortDesc