Solved

Bubble sort

Posted on 2004-09-24
8
327 Views
Last Modified: 2010-03-31
I have a question.  Does the bubble sort algorithm always guarantee a perfect sort everytime?  Or do I have a bug in my prog?

Date d1,d2;
for(int i = 0; i < d.size()-1; i++)
  for(int j = i; i < d.size()-1; i++)
    if( ( d.elementAt(j).getDt() ).before(d.elementAt(i).getDt()))
      d.setElementAt(d.elementAt(j),i);
0
Comment
Question by:JK2429
  • 8
8 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
ID: 12146156
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146183
    public int [] bubbleSort(int array[])
      {
          boolean ok=false;
          for(int i=0;i<array.length;i++)
          {
             ok=false;
             for(int j=0;j<array.length-1;j++)
             {
                if(array[j]<array[j+1])
                {
                   ok=true;
                   int temp=array[j];
                   array[j]=array[j+1];
                   array[j+1]=temp;
                }
             }
             if(!ok)break;
          }
          return array;
       }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146247
public List  bubbleSort(List array)
      {
          boolean ok=false;
          for(int i=0;i<array.size();i++)
          {
             ok=false;
             for(int j=0;j<array.size()-1;j++)
             {
               if( ( d.elementAt(j).getDt() ).before(d.elementAt(j+1).getDt()))

                {
                   ok=true;
                   int temp=array[j];
                   array[j]=array[j+1];
                   array[j+1]=temp;
                }
             }
             if(!ok)break;
          }
          return array;
       }
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146264
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146285
>> if( ( d.elementAt(j).getDt() ).before(d.elementAt(j+1).getDt()))

should be
             

 if( ( array.elementAt(j).getDt() ).before(array.elementAt(j+1).getDt()))
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146310
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146321
A vector Implementation here

 // simple bubble sort.
for (int j=myDateVector.size()-1;j>=1;j--) {
    for (int i=0;i<j;i++) {
         Date d1=(Date) myDateVector.elementAt(i);
         Date d2=(Date) myDateVector.elementAt(i+1);
         
         if (d2.getTime()<d1.getTime()) {
              myDateVector.setElementAt(d2, i);
              myDateVector.setElementAt(d1, i+1);
         }
    }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146516
Or some thing like this

/*
 * DateComparision.java
 *
 * Created on September 25, 2004, 12:24 AM
 */
import java.util.*;
/**
 *
 * @author  Sudhakar
 */
public class DateComparision {
   
    /** Creates a new instance of DateComparision */
    public DateComparision() {
        Date d=new Date();
        Date d1=new Date();
        Vector vect=new Vector();
        vect.add(d1);
        vect.add(d);
        Object[] array=vect.toArray();
        Arrays.sort(array, new DateComparator());
    }
   
}



class DateComparator implements Comparator
{
   
    public int compare(Object o1, Object o2) {
        Date d1=(Date)o1;
        Date d2=(Date)o2;
        if(d1.before(d2))
        {
            return -1;
        }
        else if(d1.equals(d2))
        {
            return 0;
        }
        else
        {
            return 1;
        }
       
   
    }
   
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 72
Error in @AspectJ Based AOP with Spring 2 18
Unhandled exception type Exception 18 43
pagenation logic how it is working in my code 1 58
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question