PDA

View Full Version : Selection sort (descending order) pseudocode check

Blowpipe
12-18-2011, 01:51 AM
Hi Everyone,

I am working on a piece of pseudocode for an assignment and as I am new to Javascript, I would like some confirmation I am on the right track with the code.

Algorithm selectionSort
Pre
a= an Array of values
n= number of items in Array
Post
a has been sorted in descending ordered from highest to lowest value

for i = 0 to n-1
max = i
for j = 0 to n-1
if a[j]>a[i-1]
max = j
end if
endfor
temp = a[j]
a[j] = a[j-1]
a[j-1]=temp
endfor

All of the research I have found has only shown ascending order formulas, so if I am wrong with my if statement and the temp statement and they should i+1 and a[j+1] can you please give a shake of the head; that way I can read my research further and edit my work.

If I am on track a nod of the head is a big encouragement.

Regards

BP

sunfighter
12-18-2011, 03:06 AM
Blowpipe, this is the javascript form. What language did you need help in?

venegal
12-18-2011, 05:20 AM
All of the research I have found has only shown ascending order formulas, so if I am wrong with my if statement and the temp statement and they should i+1 and a[j+1] can you please give a shake of the head;

That's a big shake of the head. The boundaries for the inner loop are wrong, as is the logic for getting the maximum value, and to top it off, you're constantly trying to exchange the very last value with one that doesn't exist, instead of exchanging the maximum with the leftmost value that isn't sorted yet.

I strongly suspect that you don't actually know how selectionsort works, and I recommend you look into that a bit more before trying to rewrite your code.

Also, whatever your assignment says, write real code instead of pseudocode, so you can actually have a look at what's going on.

Philip M
12-18-2011, 10:42 AM
Descending is exactly the same algorithm as ascending but with the < sign in the swap changed to >.

Have a look at http://www.nczonline.net/blog/2009/09/08/computer-science-in-javascript-selection-sort/

Blowpipe
12-18-2011, 11:35 AM
Hi Sunfighter,

The psuedocode is to create a selectionsort as opposed to a bubblesort. The unit I am working on is called 'Automate Processes' therefore I would assume Javascript is the language.

I did actually do a search for selectionsort to determine which forum to jump on to. I don't just post random questions all over the site in hope of an answer :)

BP

Blowpipe
12-18-2011, 11:40 AM
That's a big shake of the head. The boundaries for the inner loop are wrong, as is the logic for getting the maximum value, and to top it off, you're constantly trying to exchange the very last value with one that doesn't exist, instead of exchanging the maximum with the leftmost value that isn't sorted yet.

I strongly suspect that you don't actually know how selectionsort works, and I recommend you look into that a bit more before trying to rewrite your code.

Also, whatever your assignment says, write real code instead of pseudocode, so you can actually have a look at what's going on.

venegal, you are very much correct in regards to the code side of sorting. I do understand the theory of the selectionsort, but when I look at the formula my brain goes 'What the....' as I am new to programming of all types.

Unfortunately for me, my learning style requires me to understand what each variable means for me to understand overall; it took me a little while to understand about the inner and outer loops and what i, j meant so on and so forth.

But hey some of us are just a little slower on the uptake with programming than others :)

Regards

BP

Blowpipe
12-18-2011, 11:51 AM
Hi Philip,

Thanks for that and the info.. I found that link and one on bubblesort last night during my searches for the truth haha.

The reason for my i-1 or i-- was I found a vid explaining bubble sort and if you wanted to reverse the order you use the i-- to decrement the array order.

www.easyprogramming.net/tutorials

However again thank you, I will go back and look at the formulas and try to get my head around it :)

Regards

BP