Selection Sort problem

Page 1 of 1

14 Replies - 1885 Views - Last Post: 01 November 2009 - 05:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=135161&amp;s=4a064944eae7afc26caac080f83585df&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Selection Sort problem

Posted 28 October 2009 - 09:16 PM

Where am i going wrong? this is baffling me!

```public class selections {

private void selectsort(int[] a){
int large = 0;
int length = a.length-1;
int temp;
for(int i = 0; i < a.length; i++){
while(length > 0){
if(large > a[i]){
large = a[i];
}
length--;

}
temp = a[i];
a[i] = a[length];
a[length] = temp;

}
for(int b = 0; b < a.length; b++){
System.out.print(a[b] + " ");
}
}

public static void main(String[] args) {

int[] a = { 23, 45, 12, 20, 10, 4, 46 };

selections ss = new selections();
ss.selectsort(a);

}

}
```

This post has been edited by javabrijon: 29 October 2009 - 02:52 AM

Is This A Good Question/Topic? 0

Replies To: Selection Sort problem

#2 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 09:29 PM

This post has been edited by Momerath: 28 October 2009 - 09:32 PM

#3 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 10:37 PM

Momerath, on 28 Oct, 2009 - 08:29 PM, said:

should it be if(a[i] > large) { large = a[i];

how is the swap wrong?

#4 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

Re: Selection Sort problem

Posted 28 October 2009 - 11:49 PM

javabrijon, on 28 Oct, 2009 - 09:37 PM, said:

should it be if(a[i] > large) { large = a[i];

What is large the 2nd time through the outer loop?

javabrijon, on 28 Oct, 2009 - 09:37 PM, said:

how is the swap wrong?
What value does i and length have when you do the swap? Try pretending you are the computer and run through each line of code recording what each variable is at each line. Use the numbers 2, 3, 1 for your array to sort.

#5 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 01:27 AM

javabrijon, on 28 Oct, 2009 - 09:37 PM, said:

Momerath, on 28 Oct, 2009 - 08:29 PM, said:

should it be if(a[i] > large) { large = a[i];

how is the swap wrong?

definetly not getting this!..

#6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 06:33 AM

Basically, Selection Sort works by finding the maximum value in the list and swapping it with the last value. It then looks at the list, excluding the last element, repeating this process until it has reached the first element. If I was you, I would start your outer for loop at the end of the array, decrementing to 0; and change your inner loop to a for loop, iterating from 0 to length-i-1. From there, find the max element and store its index in the max field and go from there.

I actually wrote a snippet on this if you want to check it out!

This post has been edited by macosxnerd101: 29 October 2009 - 06:35 AM

#7 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 03:24 PM

macosxnerd101, on 29 Oct, 2009 - 05:33 AM, said:

Basically, Selection Sort works by finding the maximum value in the list and swapping it with the last value. It then looks at the list, excluding the last element, repeating this process until it has reached the first element. If I was you, I would start your outer for loop at the end of the array, decrementing to 0; and change your inner loop to a for loop, iterating from 0 to length-i-1. From there, find the max element and store its index in the max field and go from there.

I actually wrote a snippet on this if you want to check it out!

where can i find the snippet?

#8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 04:13 PM

Hit the link under my name says My Contributions. It will take you to a page with a list of snippets I've submitted. Selection Sort is there.

#9 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 05:03 PM

macosxnerd101, on 29 Oct, 2009 - 03:13 PM, said:

Hit the link under my name says My Contributions. It will take you to a page with a list of snippets I've submitted. Selection Sort is there.

if you didnt see i commented on your selection sort ..just mainly needto know what "i" is in the first loop.
```for(max = 1, n = x.length-1; i < n; n--){
for(int j = 0; j < n; j++){
if(x[j] > x[max]) max = j;
}
int temp = x[max];
x[max] = x[n];
x[n] = temp;
}

```

#10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 05:06 PM

Take another look at the snippet.

#11 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 05:32 PM

macosxnerd101, on 29 Oct, 2009 - 04:06 PM, said:

Take another look at the snippet.

i get an "i is unresolved" message on eclipse;

```for(max = 1, n = x.length-1; i < n; n--){
...
}
```

#12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: Selection Sort problem

Posted 29 October 2009 - 05:35 PM

It appears I made a mistake when submitting it a couple months ago, and it got past inspection (which it probably shouldn't have). I've revised it. Sorry for the inconvenience!
```public void selectionSort(int[] x){
for(int max = 1, n = x.length-1; n > 0; n--){
for(int j = 0; j < n; j++){
if(x[j] > x[max]) max = j;
}
int temp = x[max];
x[max] = x[n];
x[n] = temp;
}

}
```

#13 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 29 October 2009 - 06:39 PM

macosxnerd101, on 29 Oct, 2009 - 04:35 PM, said:

It appears I made a mistake when submitting it a couple months ago, and it got past inspection (which it probably shouldn't have). I've revised it. Sorry for the inconvenience!
```public void selectionSort(int[] x){
for(int max = 1, n = x.length-1; n > 0; n--){
for(int j = 0; j < n; j++){
if(x[j] > x[max]) max = j;
}
int temp = x[max];
x[max] = x[n];
x[n] = temp;
}

}
```

all good.

``` public class selections {

private void selectsort(int[] a){
int max;
int length;
int temp;
for(max = 1, length = a.length-1;  length > 0; length--){
for(int j = 0; j < length; j++){
if(a[j] > a[max]){
max = j;
}
}
temp = a[max];
a[max] = a[length];
a[length] = temp;

}
for(int b = 0; b < a.length; b++){
System.out.print(a[b] + " ");
}
}

public static void main(String[] args) {

int[] a = { 23, 45, 12, 20 };

selections ss = new selections();
ss.selectsort(a);

}

}
```

my array is {23, 45, 12, 20}

Quote

output: "20 12 23 45 "
.. still not working

#14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: Selection Sort problem

Posted 30 October 2009 - 09:56 AM

Sorry for the runaround. This will work:

```public static void selectionSort(int[] x){
for(int i = x.length-1; i > -1; i--){
int max = i;
for(int j = 0; j < i; j++){
if(x[j] > x[max]) max = j;
}
int temp = x[i];
x[i] = x[max];
x[max] = temp;
}//end i

}

```

#15 javabrijon

Reputation: 2
• Posts: 27
• Joined: 27-October 09

Re: Selection Sort problem

Posted 01 November 2009 - 05:51 PM

macosxnerd101, on 30 Oct, 2009 - 08:56 AM, said:

Sorry for the runaround. This will work:

```public static void selectionSort(int[] x){
for(int i = x.length-1; i > -1; i--){
int max = i;
for(int j = 0; j < i; j++){
if(x[j] > x[max]) max = j;
}
int temp = x[i];
x[i] = x[max];
x[max] = temp;
}//end i

}

```

cheers mate, funny thing is i solved it a couple days ago, same code aswell. (well after your previous code that is) gave me a good hint

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }