Results 1 to 4 of 4
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default How to change Bubble+Selection+Insertion Sorts to Sort String Values?

    Hello I am curious as to how I could modify these three sorting algorithms to sort strings instead of integers.


    Java Code:
    
    public void bubbleSort()
    {
    int out, in;
     
    for(out=nElems-1; out>1; out--)   // outer loop (backward)
       for(in=0; in<out; in++)        // inner loop (forward)
          if( a[in] > a[in+1] )       // out of order?
             swap(in, in+1);          // swap them
    }  // end bubbleSort()
     
    //--------------------------------------------------------------	
    public void selectionSort()
    {
        int out, in, min;
     
        for(out=0; out<nElems-1; out++)   // outer loop
           {
           min = out;                     // minimum
           for(in=out+1; in<nElems; in++) // inner loop
              if(a[in] < a[min] )         // if min greater,
                  min = in;               // we have a new min
           swap(out, min);                // swap them
           }  // end for(out)
        }  // end selectionSort()
     
    //--------------------------------------------------------------	
    public void insertionSort()
    {
        int in, out;
     
        for(out=1; out<nElems; out++)     // out is dividing line
           {
           long temp = a[out];            // remove marked item
           in = out;                      // start shifts at out
           while(in>0 && a[in-1] >= temp) // until one is smaller,
              {
              a[in] = a[in-1];            // shift item to right
              --in;                       // go left one position
              }
           a[in] = temp;                  // insert marked item
           }  // end for
        }  // end insertionSort()

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This question has been cross-posted by the original poster in multiple fora.

  3. #3
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    I posted my question two places....I will no do so in the future. I'm looking for multiple users input. I cannot post the same question on two different forums?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are free to do so, but imagine the frustration of anyone who takes the time and effort to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders. I would recommend against it, or in the very least, be up front about it and post links.

Similar Threads

  1. Trouble w/ Bubble Sort
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:41 PM
  2. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  3. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM
  4. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:27 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •