1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

Logic Challenge!

Discussion in 'Other Stuff' started by GoldenMouse, May 20, 2005.

  1. GoldenMouse

    GoldenMouse Mad Hatter

    Joined:
    Nov 14, 2001
    Messages:
    2,011
    Likes Received:
    0
    Using pseudo-code (computer instructions written in plain english) outline the steps to take an array of variable size and order the contents from highest to lowest.
     
  2. GoldenMouse

    GoldenMouse Mad Hatter

    Joined:
    Nov 14, 2001
    Messages:
    2,011
    Likes Received:
    0
    Really, this is just a thinly veiled attempt to get some help. :D
     
  3. Bot_40

    Bot_40 Go in drains

    Joined:
    Nov 3, 2001
    Messages:
    2,914
    Likes Received:
    0
    Heh, it's a large research area...efficient sorting algorithms. If you are looking for simplicity rather than speed then bubble sort will prolly do the job.
    You basically go up and down the array over and over comparing each pair of items, if they are the wrong way around then swap them, otherwise skip over. Eventually the list will be sorted. You know you are finished when you do an entire pass without making any swaps.
    It's pitifully slow when you have to sort many items (proportional to the square of the nubmer of items in the array), but prolly the easiest algorithm to code.

    Another easy one is selection sort. Simply keep scanning through the array and each time find the next smallest (or largest) item and move it to the back (or front depending which way you want it sorted). Again it's rather slow if you have many items (again, square of the number of items in the array but slightly faster than bubble sort iirc)

    The faster algorithms generally are quite a lot harder to understand (and I am crap at explaining stuff so I won't even try at this point). Shellsort and quicksort are 2 of the fastest but quite impossible for me to explain (I never actually really got my head around quicksort). Have a look on google, there's absolutely tons of stuff on this around and I actually find it quite interesting. It's one of those things which seems like it should be rather easy to do for a human, but if you are a computer that can only compare 2 items at a time it's a hell of a lot harder.
     
  4. GoldenMouse

    GoldenMouse Mad Hatter

    Joined:
    Nov 14, 2001
    Messages:
    2,011
    Likes Received:
    0
    Heh, thanks. I'm just starting to learn code. i.e. I toyed with Basica in elementary, then had nothing whatsoever to do with any code until this quarter, when I took a class on VB and have been independently going through a book on C. I plan on learning more advanced stuff once I get through this intro material. Long story short, I'm a noob to it right now. Now, I was playing risk one or two nights ago and thought that it'd be rather easy to code, once I picked up the necessary skills. In fact, I could probably write the function to handle battles.... So I'm now exactly where I want to be, stumbling on stuff that I'd never have stumbled upon without doing a project of sorts.

    So this is what I found. I'm having a hard time breaking it down, though it uses no unfamiliar functions or syntax.
     
    Last edited: May 20, 2005
  5. bobtheking

    bobtheking Monkey in a bucket

    Joined:
    Dec 1, 2001
    Messages:
    1,237
    Likes Received:
    0
    what language are you using? if you are doing this as part of another project, you should probably use the ones that come with the language (they will invariably be better and faster than anything almost all programmers could write in a reasonable amount of time)

    in C there is quicksort, C++ has a bunch of ways (check out the algorithm header), .Net languages some of the collections have a Sort method.

    if you are doing it for learning, i would start with a bubble sort as bot_40 described. quicksort actually isn't too hard to implement, the basic idea is actually really simple. http://en.wikipedia.org/wiki/Quicksort is a really good article on it, including pseudocode and actual implementations in quite a few languages.
     
  6. GoldenMouse

    GoldenMouse Mad Hatter

    Joined:
    Nov 14, 2001
    Messages:
    2,011
    Likes Received:
    0
    Very nice.

    I have a headache.
     
  7. GoldenMouse

    GoldenMouse Mad Hatter

    Joined:
    Nov 14, 2001
    Messages:
    2,011
    Likes Received:
    0
    Ok, I have the C source for quicksort. I'm going to try to go through and comment it. See how my comments compare to how you understand things to be. For the sake of brevity, we'll assume the swap function to be self-explanatory.

    Line 1: Accepts arguments where arr is the array to be sorted, beg is the beginning of the segment to be sorted, and end is the end of the segment to be sorted.
    Line 3: Checks size of segment to be sorted to see if it has more than two values. More than two values, sort the segment. Otherwise, consider them sorted and stop the recursion.
    Line 5: Initializes working variables where piv is the pivot value, L is the current item being compared, and r is the end of the working set. The pivot takes the value of the first cell to be sorted, which is random for all intents and purposes.
    Line 6: Loop until working set has been sorted.
    Lines 8-11: If the value of the cell in L position is less than or equal to the pivot, do nothing and move to the next cell. Otherwise, take that value and move it to the end of the set, decreasing the upperboundary of the working set by one. This segregates values in the segment into ones greater than the pivot and less than the pivot. Note that those two subsets are disordered.
    Line 13: Swaps the values of the cell right before the end of the working set and the cell at the beginning of the segment. (Why?)
    Line 14: Repeat this process for the smaller subset.
    Line 15: Repeat this process for the larger subset.
     
    Last edited: May 22, 2005
  8. Twrecks

    Twrecks Spectacularly Lucky

    Joined:
    Mar 6, 2000
    Messages:
    2,606
    Likes Received:
    1
    new[]=order[array[max-min]]

    Of course thi doesn't do squat unless you make your own operands
     

Share This Page