Solved

Bubble sort

Posted on 2004-09-24
8
324 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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12146264
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now