Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Selection Sorting

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Selection Sorting

    im having problem with this one....

    public class SelectionSorting {
     
        public static void main(String[] args) {
     
            int[] number = {8, 1, 10, 5, 2, 9, 4, 6, 7, 3};
     
            for (int x = number.length - 1; x >= 0; x--) {
     
                for (int a = 0; a <= number.length - 1; a++) {
     
                    if (number[x] < number[a]) {
     
                        int temp = number[x];
     
                        number[x] = number[a];
                        number[a] = temp;
                    }
                }
            }
     
            System.out.println(number[0]);
            System.out.println(number[1]);
            System.out.println(number[2]);
            System.out.println(number[3]);
            System.out.println(number[4]);
            System.out.println(number[5]);
            System.out.println(number[6]);
            System.out.println(number[7]);
            System.out.println(number[8]);
            System.out.println(number[9]);

    the output is as follows:
    10
    1
    2
    3
    4
    5
    6
    7
    8
    9


    but when i change the INNER Loop...(which will iterate to compare the values) ,
    instead of starting from "0"-zero, i started from the array's length.

    public class SelectionSorting {
     
        public static void main(String[] args) {
     
            int[] number = {8, 1, 10, 5, 2, 9, 4, 6, 7, 3};
     
            for (int x = number.length - 1; x >= 0; x--) {
     
                for (int a = number.length - 1; a >= 0; a--) { // this is the part that i've modified
     
                    if (number[x] > number[a]) {
     
                        int temp = number[x];
     
                        number[x] = number[a];
                        number[a] = temp;
                    }
                }
            }
     
            System.out.println(number[0]);
            System.out.println(number[1]);
            System.out.println(number[2]);
            System.out.println(number[3]);
            System.out.println(number[4]);
            System.out.println(number[5]);
            System.out.println(number[6]);
            System.out.println(number[7]);
            System.out.println(number[8]);
            System.out.println(number[9]);
        }
    }

    the output of this code is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10



    i cant figure out how did it happen....


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Selection Sorting

    void selectionSort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[min]) {
                    min = j;
                }
            }
            if (i != min) {
                int swap = a[i];
                a[i] = a[min];
                a[min] = swap;
            }
        }
    }

    Wiki's Solution in Java, study it
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Selection Sorting

    whahahahahah i know its not that easy to explain why..... so thats why im holding a pen and a paper now.. just to analyze bit by bit how does it happpen hwhahhahaha , im starting to get a head pain....

    .. anyway chris tnx for that!

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Selection Sorting

    ok ill explain it in words for you.

    set min to first value in array.
    loop through, if a number is less than min set min to that number
    at the end of the array swap the first element in the array with the lowest number found.
    repeat but ignoring element one in the array
    and so on

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (December 9th, 2009)

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Selection Sorting

    public class SelectionSorting {
     
        public static void main(String[] args) {
     
            int temp;
     
            int[] number = {8, 1, 7, 3, 9, 4, 10, 5, 6, 2};
     
            for (int x = 0; x <= number.length - 1; x++) {
     
                for (int a = 0 ; a <= number.length - 1; a++) {
     
                    temp = number[x];
                    number[x] = number[a];
                    number[a] = number[x];
     
                    if (x == 0) {
     
                        break;
                    }
                }
     
            }
     
            for (int output = 0; output <= number.length - 1; output++) {
     
                System.out.println(number[output]);
            }
        }
    }

    why is the output like this>>
    8
    2
    2
    2
    2
    2
    2
    2
    2
    2

    it should be STILL {8, 1, 7, 3, 9, 4, 10, 5, 6, 2},

    why is the inner loop continuingly or still assigning the last value in the next indexes after the first index(0)?
    it supposed to STOP, because I stated a 'break' statement, that IF X == 0 , then stop assiging values...


    it should only affect the first index(0)...
    it should stop on the index[a] where a == 0;
    it supposed to stop incrementing, because it will check if x == 0, then if it is.. 'break' the loop.


    i hope you understand what im trying to explain...
    why?
    Last edited by chronoz13; December 10th, 2009 at 06:24 AM.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Selection Sorting

    Here's the general premises of a selection sort algorithm: Take a list, then find the minimum. Put that value at the beginning of the list. Then, take the rest of the list and repeat those steps (of course, you don't put them at the beginning of the whole list, just the beginning of the partial list).

    You're getting all 2's after because your swap algorithm isn't actually swapping the values. Here's what it's doing:

    say number[x] = 5 and number[a] = 3
    temp = number[x]; // temp = 5
    number[x] = number[a]; // number[x] = 3
    number[a] = number[x]; // number[a] = 3

    What that last line really should be is number[a] = temp;

    This will still cause problems though, because values are getting swapped for no reason what-so ever. What you really should have inside is a find minimum algorithm, then in your outer for loop perform the appropriate swap:

    for (int x = 0; x < number.length; i++)
    {
         int minIndex = x;
         for (int a = x; a < number.length; a++)
         {
              if(number[minIndex] > number[a])
              {
                   // found a smaller value in the remaining list
                   minIndex = a;
              }
         }
         // minIndex now has the index of the smallest number in the remaining list
         int temp = number[minIndex];
         number[minIndex] = number[x];
         number[x] = temp;
    }

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 10th, 2009)

Similar Threads

  1. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  2. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 03:38 AM
  3. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 07:25 AM
  4. Replies: 4
    Last Post: June 10th, 2009, 02:04 AM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:08 PM