Tuesday, October 25, 2016

Java Program to Bubble Sort

Bubble Sort is a simple sorting algorithm. Here sort means arranging the elements in one particular order. That means either ascending or descending order.It is one of the sorting technique.This sorting algorithm is comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order.This algorithm is not suitable for large data. We can do the fallowing

  • Compare Two numbers at a time that means starting with First Two numbers
  • If the Top number is greater than second number then exchange those two numbers
  • Go down One number and compare that number to the next number
  • Continue this process until no exchange has been made.
 Algorithm:

Step 1 : Start
Step 2:  Set i=0;
Step 3 : Repeat the steps 4,5,6 while(i<(n-1))
Step 4:  Set j=0;
Step 5: Repeat the steps 6,7 while(j<(n-(i+1))
Step 6: if(a[j]>a[j+1])then swap the values
Step 7: j=j+1;
Step 8: i=i+1;
Step 9: Stop

How Bubble sort works:

We take an unsorted array for our example.
Bubble Sort starts with very first two elements,comparing them to check which one is greater.

In this case value 23 is greater than 13,So it is already in sorted locations.Next, we compare 23 with 18

In this case we find that 18 is smaller than 23 and these two values must be swapped.After swapping The new array look like as shown below:
Next we compare 23 with 35. We find that both are already sorted positions. So no need to swap.
Then we move to next two  values,35 and 8. We know 8 is smaller than 35.Hence they are not sorted.
Now we need to swap these two values. We find we reach at the end of the array.After one iteration the array should look as shown below:
To be precise,we are now showing that how array should  look like after each iteration. After second iteration,it should look like below:
Notice that after each iteration, at least one value moves at the end.
And there is no swapping required,bubble sorts learns that array is completely sorted.

Java Bubble Sort Program:

import java.io.*;
class Sort
{
public static void main(String args[])throws IOException
{
//to accept data from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//create int type array
System.out.println("How many elements?");
int n=Integer.parseInt(br.readLine());
int arr[]=new int[n];
//accept integer elements into the array
for(int i=0;i<n;i++)
{
System.out.println("enter int:");
arr[i]=Integer.parseInt(br.readLine());
}
//use bubble sort technique to sort the integers
int limit=n-1;//elements from 0 to n-1
boolean flag=false;//if it is true swapping done
int temp;//temporary variable 
for(int i=0;i<limit;i++)
{
for(int j=0;j<limit-i;j++)
//if first element is bigger than second one then swap
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=true;//true means swapping done
}
}
//displaying sorted array
System.out.println("The sorted array is:");
for(int i=0;i<n;i++)

System.out.println(arr[i]);
}
}
Output:

E:\javaprograms>javac Sort.java
E:\javaprograms>java Sort

How many elements?
5
enter int:
23
enter int:
43
enter int:
8
enter int:
12
enter int:
2
The sorted array is:
2
8
12
23
43

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...