Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Bubble Sorting in Java

                         

Introduction

In this example we are going to sort integer values of an array using bubble sort.

Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follow the same steps repeatedly until the values of array is sorted. In worst-case the complexity of bubble sort is O(n2) and in  best-case the complexity of bubble sort is Ω(n).
      
Code description:

Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at higher index. Then we take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again  from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not sorted. 

Working of bubble sort algorithm:

Say we have an array unsorted  A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array.
 1.Compare A[0] and A[1] . 
 2.If A[0]>A[1] then Swap A[0] and A[1].
 3.Take next A[1] and A[2].
 4.Comapre these values.
 5.
If A[1]>A[2]  then swap A[1] and A[2]
 ...............................................................
................................................................
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.

In our example we are taking the following array values
12 9 4 99 120 1 3 10

The basic steps followed by algorithm:-
  
In the first step compare first two values 12 and 9.
12 9 4 99 120 1 3 10

As 12>9 then we have to swap these values 
Then the new sequence will be 
9 12 4 99 120 1 3 10

In next step take next  two values 12 and 4 
9 12 4 99 120 1 3 10

Compare these two values .As 12>4 then we have to swap these values. 
Then the new sequence will be
9 4 12 99 120 1 3 10

We have to follow similar steps up to end of array. e.g.
9 4 12 99 120 1 3 10
9 4 12 99 120 1 3 10
9 4 12 99 1 120 3 10 
9 4 12 99 1 120 3 10
9 4 12 99 1 3 120 10
9 4 12 99 1 3 10 120

When we  reached at last index .Then restart same steps unlit the data is not sorted.

The output of this example will be :
1 3 4 9 10 12 99 120 

The code of the program :

public class bubbleSort{
  public static void main(String a[]){
    int i;
    int array[] {12,9,4,99,120,1,3,10};
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.printarray[i]+"  ");
    System.out.println();
    bubble_srt(array, array.length);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void bubble_srtint a[]int ){
    int i, j,t=0;
    for(i = 0; i < n; i++){
      for(j = 1; j < (n-i); j++){
        if(a[j-1> a[j]){
          t = a[j-1];
          a[j-1]=a[j];
          a[j]=t;
        }
      }
    }
  }
}

Output of the example:

C:\array\sorting>javac bubbleSort.java
C:\array\sorting>java bubbleSort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
1  3  4  9  10  12  99  120
PAUSE
C:\array\sorting>_

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
Tiles definition richf
column name in panel g
jar entry
PHP Web Traffic Analys
java program to check
ะ�?????ะ�?????
java programm for arra
how to call pl/sql fro
logice
iterator
sleep
S[ring
Photoshop Photo Effect
Image with mouse drag
The JDBC(tm) Universal
Image panel should pro
å???å????å????å???å???
X10 Client and Server
delete data in mysql d
rs.relative
Button
setPropertyActionListe
jQuery To Hide the Div
hibernate many to many
break?ะ�ะ???????
Photoshop Basics Using
Photoshop Drawing Cand
insert column
ASP EXAMPLE' and 1=1 a
line break in javascri
java functions
spring portlet
draw tree
display data from mysq
passing parameters to
CLDC Chechbox example
dynamic table
javascript DB servl
Account superclass and
close a frame
java cancel button
which design patterns
tiles in structs
Photoshop Basics Draw
JavaScript functions o
partially selected che
get data by using char
java inner class
JavaScript functions
Arrays
html:frame
popup in jsp
c tutorial
ready made component o
Struts1.1
Developing Struts Hibe
Jar files in java
connectivity of ms sql
split
JavaScript
differences of java to
optiontransferselect
using javascripting in
how can we select an d
hibernate2 Tomcat5.5 s
create action listener
FlowLayout
Flex Java Tutorial
rotate
factorial applet
input in array
Create Scroll Bar in J
webse
Javascript Windows and
draw triangle
iterate nest
difference between arc
create box in excel sh
how to install tomcat
weblogic server
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.