Click to See Complete Forum and Search --> : in need of help


Liphoto
10-19-2005, 02:04 AM
Can anyone please help. I would like to have an algorithm or pseudocode for a selection sort. In giving me posible solitions assume that i already have a function maximum which obtains the largest number in an array.
I would like to switch the largest number in the array with the last number in the array and no do any switch when the numbers are equal. And with every pass i woule like to concider all the numbers exept the number which is the larger; the one which has been swapped already.

for example, if my array is [4,3,2,1]
since 4 is the largest i will swap it with 1: then hace [1,3,2,4] as my array. Then from only the first three numbers find the maximum then swap if necessary. Looking at my array i would have 3 as the larges of the three (1,2,3).
I should ba able to keep doing this till the array is all sorted.

Your help will be greatly appreciated

Fang
10-19-2005, 02:44 AM
Sounds like a simple number sort:
<script type="text/javascript">
var list=[4,3,2,1];
function sortNumbers(a, b) {
return a - b;
}
alert(list.sort(sortNumbers));
</script>