# Selection Sorting

• December 9th, 2009, 04:40 AM
chronoz13
Selection Sorting
im having problem with this one....

Code :

```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:
Code :

```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.

Code :

```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:
Code :

```1 2 3 4 5 6 7 8 9 10```

i cant figure out how did it happen....
• December 9th, 2009, 04:55 AM
Freaky Chris
Re: Selection Sorting
Code :

```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 :D
• December 9th, 2009, 05:05 AM
chronoz13
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! :))
• December 9th, 2009, 06:25 AM
Freaky Chris
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
• December 10th, 2009, 05:20 AM
chronoz13
Re: Selection Sorting
Code :

```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>>
Code :

```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... :confused:
why?
• December 10th, 2009, 10:08 AM
helloworld922
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
Code :

```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:

Code :

```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; }```