Solved

Bubble sort

Posted on 2004-09-24
8
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

690 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