Solved

SelectionSort and BubbleSort algorithims

Posted on 2004-10-31
242 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
Question by:NeedlessKane
    12 Comments
     
    LVL 13

    Expert Comment

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

    Collections.sort(urCollection like Vector or List);

    ?
    0
     
    LVL 92

    Expert Comment

    by:objects
    0
     

    Author Comment

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

    0
     
    LVL 92

    Expert Comment

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

    Author Comment

    by:NeedlessKane
    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
    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
    > 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
    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
    //-------------------------------------------------- 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:
    0
     
    LVL 14

    Expert Comment

    by:sudhakar_koundinya
    >> 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
    I figured out the problem for myself, thanks for all who posted.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Title # Comments Views Activity
    seriesUp challenge 7 50
    Problem to setup 18 55
    userCompare  challenge 3 36
    Performance issue while iterating with streams 5 23
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This video teaches viewers about errors in exception handling.

    933 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

    16 Experts available now in Live!

    Get 1:1 Help Now