Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Selection sort (descending order) pseudocode check

    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

    Code:
    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

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,030
    Thanks
    23
    Thanked 589 Times in 588 Posts
    Blowpipe, this is the javascript form. What language did you need help in?

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by Blowpipe View Post
    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.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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/0...election-sort/

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    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

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •