I need help with a selection sort
Author |
Message |
Genisis
|
Posted: Wed Jan 11, 2006 11:06 am Post subject: I need help with a selection sort |
|
|
i dont know how to selection sort (5 numbers lowest to highest,with one array)i know how it works but i dont know how to type in code will someone help me out
this is wat i got so far:
code: |
var num : array 1 .. 5 of int
var temp: int
for i : 1 .. 5
get num (i)
end for
temp := num (1) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
|
|
|
![](images/spacer.gif) |
CooKieLord
|
Posted: Mon Jan 23, 2006 5:52 pm Post subject: (No subject) |
|
|
Tony wrote: Wikipedia has an example in C
I'm sorry for posting on this "old" thread, but I need help with the sorting as well. I was reading through the example in C but I really don't understand.
Can you help me out here? |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Mon Jan 23, 2006 6:34 pm Post subject: (No subject) |
|
|
for example, u have 3 numbers that u want to sort and they are
2
3
1
1) check if 1<3=true?
2) 1<3 is true
3) check if 1<2=true?
4) 1<2 is true
5) 1 is now the first number in the sequence, and 2 is the last number in the sequence
now the new sequence is:
1
3
2
6) check if 2<3=true?
7) 2<3 is true
8) 2 is now the second number in the sequence, and 3 is the last number in the sequence
final result:
1
2
3 |
|
|
|
|
![](images/spacer.gif) |
CooKieLord
|
Posted: Mon Jan 23, 2006 6:47 pm Post subject: (No subject) |
|
|
For steps 1 and 3, does it check the value or the array number (like number(x) )?
I'm guessing array number.
I've also discovered Bubble Sort, wich seems to fit my need.
However I haven't decifered how it works yet.
code: | var num : array 1 .. 5 of int
var temp : int
put "Enter 5 numbers:" ..
for x : 1 .. 5
get num (x)
end for
put skip
put "The five numbers you've entered are :" ..
for x : 1 .. 5
put num (x)
end for
for x : 1 .. 5
for y : 1 .. 4
if num (x) < num (y) then
temp := num (x)
num (x) := num (y)
num (y) := temp
end if
end for
end for
put skip
put "The five numbers in order are:"
for x : 1 .. 5
put num (x)
end for |
Is it possible to explain the swapping process in another way? |
|
|
|
|
![](images/spacer.gif) |
person
|
Posted: Mon Jan 23, 2006 7:11 pm Post subject: (No subject) |
|
|
Bubble Sort: in a sequence of a,b,c,d. It only swaps with adjusent letters.
e.g. c can only switch with b and d
Selection Sort: in a sequence of a,b,c,d. It swaps the value of a letter with any letter, and it will never be checked again.
e.g. a swaps with c, then a will never be checked again since it has acquired the correct value |
|
|
|
|
![](images/spacer.gif) |
blaster009
|
Posted: Mon Jan 23, 2006 8:50 pm Post subject: (No subject) |
|
|
Alright...Now for a proper explanation in English.
Bubble Sort: Why is it called Bubble Sort? An odd name for a computer pattern. Well, it is so named because Bubble Sort happens to resemble bubbles floating to the top of a pond. What Bubble sort does is it takes the largest number it can find, and continually moves it towards the end by swapping it with the number next to it. However, when bubble sort finds an even LARGER number, it will abandon the original one, and bring the new biggest number to the top, like so:
6,3,4,9,5,1 <---- Shift 3 and 6
3,6,4,9,5,1 <---- Shift 4 and 6
3,4,6,9,5,1 <---- Abandon 6, because 9 > 6. Shift 9 with 5
3,4,6,5,9,1 <---- Shift 9 with 1
3,4,6,5,1,9 <---- Now, we know that the highest number is at the top of the list, so we start all over again, and move numbers until we reach the second last position in the list, and so on, until we are only moving one number (which means the list is sorted).
Someone else wanna take Selection? I'm a little short on time... |
|
|
|
|
![](images/spacer.gif) |
person
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
CooKieLord
|
Posted: Fri Jan 27, 2006 9:51 am Post subject: (No subject) |
|
|
Oh I understand completely now, thanks alot for your efforts and explanations ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|