Solved

Bubble sort

Posted on 2004-09-24
8
326 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 48
jar file executable 12 51
pagenation logic how it is working in my code 1 27
Is there a simpler dropbox system? 10 16
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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:

785 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