# write a selection sort without having numerous variable?

• 10-28-2008, 04:05 AM
seandingobat
write a selection sort without having numerous variable?
i'm trying to do a selection sort to sort out an array of unsorted numbers.

basically just trying to swap their position in the index.

for example here:

for( int x = ; x < list.length; x ++){
for( int i = x; i < list.length; i ++){
if( list[x] > list[i] ){
list[x] = list[i];
list[i] = list[x];
}
}
}
return list;

i know that this is wrong, but i hope you get the idea. i'm complete lost here.

how do i make it so that it makes sure that it checks all the way through the list first and not just the first one that it sees that fits the condition?

then how can you swap their value if the value of list[x] has already changed to list[i]?
• 10-28-2008, 04:14 AM
emceenugget
There is a Wikipedia article for this with pseudocode. It doesn't get much easier than that.
• 10-28-2008, 09:18 AM
Eranga
Quote:

Originally Posted by emceenugget
There is a Wikipedia article for this with pseudocode. It doesn't get much easier than that.

Can you post the wiki link here.
• 10-28-2008, 02:49 PM
Norm
Quote:

list[x] = list[i];
list[i] = list[x];
To do a swap of contents of two variables you need a third variable to hold the value.
The above will make the two array elements have the same content.
The first line copies [i] to [x] making them both the same, losing the original contents of [x]. You need to save [x] in a temp so you can put store it into [i] next. Take a piece of paper and do the steps one at a time to see.
• 10-28-2008, 03:02 PM
Eranga
Norm, is that clear what our OP is going to do? Trying to swap two consecutive numbers or else.
• 10-28-2008, 03:24 PM
Norm
Eranga,
I'm just guessing. The code looked like a common mistake when trying to swap two variables.
Without comments (Why don't teachers insist on coments???)
there is no way to know what the OP is trying to do.
• 10-28-2008, 03:33 PM
Eranga
Quote:

Originally Posted by Norm
Why don't teachers insist on coments???

I'm worrying that too Norm.

I thought you have find some kind of a pattern from the first post.