Solved

Bubble sort

Posted on 2004-09-24
8
325 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
network + 7 80
Securing Jmx Console and web console 2 64
eclipse java  build path 6 44
MySqlDump not dumping triggers 1 15
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

23 Experts available now in Live!

Get 1:1 Help Now