SelectionSort and BubbleSort algorithims

Hello,

I need the source code for BubbleSort and SelectionSort algorithims that take in a Comparable array as follows:

public static void BubbleSort(Comparable[] a)
{

// code here

}

public static void SelectionSort(Comparable[] a)
{

//code here

}

Thanks

-Taylor
NeedlessKaneAsked:
Who is Participating?
 
petmagdyCommented:
why don't implement Comparable interface in ur class and use:

Collections.sort(urCollection like Vector or List);

?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
NeedlessKaneAuthor Commented:
I tried that link earlier objects, I can't get the code to compile.

0
 
objectsCommented:
post the problems, and i'll help resolve them.
0
 
NeedlessKaneAuthor Commented:
Ok, well I imported the SortAlgorithim and the SortItem clases, and when I compile SortItem it says i have a depreciated API.  

0
 
NeedlessKaneAuthor Commented:
All i need is  a simple algorithim that uses a Comparable array, I thought it'd be easy to find one online, but my searching has turned up nothing.

0
 
objectsConnect With a Mentor Commented:
> it says i have a deprecated API

thats only a waring, you can ignore that.
to find the particular lines compile with -deprecation option
0
 
sudhakar_koundinyaConnect With a Mentor Commented:
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;
       }
      public int [] bidirectionalSort(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(array[array.length-(j+2)]<array[array.length-(j+1)])
                {
                   ok=true;
                   int temp=array[array.length-(j+2)];
                   array[array.length-(j+2)]=array[array.length-(j+1)];
                   array[array.length-(j+1)]=temp;
                }                                                
             }            
             if(!ok)break;
          }
          return array;
       }
      public int [] BidirectionalSort(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;
                }
             }
             for(int j=0;j<array.length-1;j++)
             {

                if(array[array.length-(j+2)]<array[array.length-(j+1)])
                {
                   ok=true;
                   int temp=array[array.length-(j+2)];
                   array[array.length-(j+2)]=array[array.length-(j+1)];
                   array[array.length-(j+1)]=temp;
                }                                                
             }            
             if(!ok)break;
          }
          return array;
       }
      public int [] BidirectionalSort1(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)
                if(array[array.length-(j+2)]<array[array.length-(j+1)])
                {
                   ok=true;
                   int temp=array[array.length-(j+2)];
                   array[array.length-(j+2)]=array[array.length-(j+1)];
                   array[array.length-(j+1)]=temp;
                }                                                
             }            
             if(!ok)break;
          }
          return array;
       }
0
 
sudhakar_koundinyaCommented:
//-------------------------------------------------- sort()
// Sort a String array using selection sort.
void sort(String[] a) {
  for (int i=0; i<a.length-1; i++) {
     for (int j=i+1; j<a.length; j++) {
        if (a[i].compareTo(a[j]) > 0) {
           String temp=a[j]; a[j]=a[i]; a[i]=temp;
        }
     }
  }
}
0
 
sudhakar_koundinyaCommented:
>> it says i have a deprecated API

No worries as said by objects. that are related to Thread and awt methods.

SortItem.java:81: warning: size() in java.awt.Component has been deprecated
        int a[] = new int[size().height / 2];
                          ^
SortItem.java:82: warning: size() in java.awt.Component has been deprecated
        double f = size().width / (double) a.length;
                   ^
SortItem.java:153: warning: size() in java.awt.Component has been deprecated
        int y = size().height - 1;
                ^
SortItem.java:158: warning: size() in java.awt.Component has been deprecated
            g.drawLine(arr[i], y, size().width, y);
                                  ^
SortItem.java:163: warning: size() in java.awt.Component has been deprecated
        y = size().height - 1;
            ^
SortItem.java:171: warning: size() in java.awt.Component has been deprecated
            g.drawLine(0, y, size().width, y);
                             ^
SortItem.java:176: warning: size() in java.awt.Component has been deprecated
            g.drawLine(0, y, size().width, y);
                             ^
SortItem.java:213: warning: stop() in java.lang.Thread has been deprecated
                kicker.stop();
                      ^
8 warnings
0
 
NeedlessKaneAuthor Commented:
I figured out the problem for myself, thanks for all who posted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.