Link to home
Start Free TrialLog in
Avatar of NeedlessKane
NeedlessKane

asked on

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
Avatar of petmagdy
petmagdy
Flag of Canada image

why don't implement Comparable interface in ur class and use:

Collections.sort(urCollection like Vector or List);

?
Avatar of Mick Barry
Avatar of NeedlessKane
NeedlessKane

ASKER

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

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

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.

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
//-------------------------------------------------- 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;
        }
     }
  }
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
I figured out the problem for myself, thanks for all who posted.