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

Bidirectional Bubble Sort in Java

                         

Introduction : Bidirectional Bubble Sort

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

Definition:

A alternative of bubble sort is bi-directional bubble sort. The  bi-directional bubble sort compares each adjacent pair of elements in an array. The values will be swap if necessary. The values passes from the beginning to the end and also from the end to the beginning. It stops when there is no any element to swap.
Bi-directional bubble sorting also known as cocktail shaker sort, shaker sort, double-direction bubble sort. The complexity of bi-directional bubble sort is O(n2).Bi-directional bubble sort is  better than bubble sort. In Bi-directional bubble sort at least one elements is moved forward or backward to its place in the array with each pass. But in the case of bubble sort moves elements by forward direction to its place, but can only move elements backward in only one location in each pass.
      
Working of bi-directional 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 bi-directional bubble sort algorithm to sort the values of an array.
 1.Compare A[0] &A[1] and  A[n-1] & A[n] . 
 2.If A[0]>A[1] then Swap A[0] & A[1] and also if A[n-1]>A[n] then swap it.
 3.Take next A[1] & A[2] and A[n-2] & A[n-1].
 4.Comapre these values.
 5.If A[1]>A[2] then Swap A[1] & A[2] and also if A[n-2]>A[n-1] then swap it.

 ...............................................................
................................................................
Stop: when there is no any  pass for swap or the array values are in sorted order.

The code of the program :

public class BidirectionalBubbleSort{
  public static void main(String a[]){
    int i;
    int array[] {12,9,4,99,120,1,3,10};
    System.out.println("\n\n       RoseIndia\n\n");
    System.out.println("       Selection Sort\n\n");
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.printarray[i]+"  ");
    System.out.println();
    bidirectionalBubble_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 bidirectionalBubble_srt(int array[]int n){
    int j;
    int st = -1;
    while (st <  n) {
      st++;
      n--;
      for (j = st; j <  n; j++) {
        if (array[j> array[j + 1]) {
          int T = array[j];
          array[j= array[j + 1];
          array[j + 1= T;
        }
      }
      for (j =  n; --j >= st;) {
        if (array[j> array[j + 1]) {
          int T = array[j];
          array[j= array[j + 1];
          array[j + 1= T;
        }
      }
    }
  }
}

Output of the example:

C:\array\sorting>javac BidirectionalBubbleSort.java
C:\array\sorting>java BidirectionalBubbleSort
       RoseIndia
       Selection Sort
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:
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
what is a bean?
panelGrid/
find a replace bytes i
adding new cell to ex
how to put a image in
align checkboxlist in
Java tutorials arrays
image file upload jsp
bufferedImage
uses of wi fi
Get singleCharFromStri
log4j configuration f
java arraylist
examples for email val
prime numbers between
dojo.css
error message and stru
Retrieve data from dat
scale
read a file on the ser
Photoshop Web Graphics
clear case and tomcat
helloe world servlet
poc dojo container
д??????д?????д????Ð
save image to file
PHP Introduction to PH
list switch
lucene in windows
binary recursio
java program to arrang
print values from tabl
java math random
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.