[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SelectionSort and BubbleSort algorithims

Posted on 2004-10-31
12
Medium Priority
?
259 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:NeedlessKane
[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
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12459074
why don't implement Comparable interface in ur class and use:

Collections.sort(urCollection like Vector or List);

?
0
 

Author Comment

by:NeedlessKane
ID: 12459147
I tried that link earlier objects, I can't get the code to compile.

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 92

Expert Comment

by:objects
ID: 12459163
post the problems, and i'll help resolve them.
0
 

Author Comment

by:NeedlessKane
ID: 12459213
Ok, well I imported the SortAlgorithim and the SortItem clases, and when I compile SortItem it says i have a depreciated API.  

0
 

Author Comment

by:NeedlessKane
ID: 12459248
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
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12459292
> 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
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 180 total points
ID: 12459390
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12459399
//-------------------------------------------------- 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
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 180 total points
ID: 12459408
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12459496
>> 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
 

Author Comment

by:NeedlessKane
ID: 12468004
I figured out the problem for myself, thanks for all who posted.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

656 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