Thread: Pdeuso code merge sort

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    11

    Question Pdeuso code merge sort

    Hello,

    I got a pseudo code and there are a few things I don't quite understand :)

    First of all, here is the code:

    Code:
    1 IterativeMergeSort (Array A, s, n)
    2     step ← 1
    3     while    step ≤ n do
    4          left ← 1
    5          while    left ≤ n − step    do
    6              middle ← left + step − 1
    7              middle ← min (middle, n)
    8              right ← left + 2*step − 1
    9              right ← min (right, n)
    10             merge (A, left, middle, right)
    11             left ← left + 2*step
    12        step ← step *2
    My function looks like this:

    Code:
    void merge_sort(int* array, int first, int last)
    Okay, first I thought that left and right (pseudo code) were equal to first and last (code). But seeing as the peudo code uses the n, which is definitely equal to my last, do I have to initialize left, middle and right?
    However, if I do this (and the pseudo code also looks like this), then I don't use s/first. It just doesn't appear at all!
    What most confuses me is "min". It appears to be a function that I call, but which function? What does "min" stand for? Anyone have an idea? For now I tried a few things and just left it out but my code doesn't work so I feel like maybe I shouldn't ignore those two lines. :/
    Thank you for any tipps on where I am wrong! :)
    Every other function is working so I feel like the problem is in my inability to read the pseudo code.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ma_ry
    What most confuses me is "min". It appears to be a function that I call, but which function? What does "min" stand for? Anyone have an idea?
    "min" means minimum, and you can imagine that min is this algorithm:
    Code:
    min (x, y)
        if x < y
            result ← x
        else
            result ← y
        return result
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Without seeing the rest of your code, it's hard to say how badly it matches the pseudo code.

    But to answer your basic question, pseudo code will leave out an awful lot you need to explicitly state in C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2018
    Posts
    11
    You are always so helpful!
    I guessed that but I thought they would have said minimum instead so I wasn't sure. Do I see it right, that s/first doesn't get used at all?

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    11
    I am still learning and it is so hard for me! I only do this for like three months now so I still need to get used to pseudo codes. There may be something implied that is easy for other people to read but I don't see it, that is why I am asking here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone tell me what's wrong with my Merge Sort Code?
    By Lezou Dali in forum C Programming
    Replies: 2
    Last Post: 01-31-2014, 10:53 AM
  2. Merge sort fails to sort
    By Cresh07 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 11:17 AM
  3. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  4. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  5. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM

Tags for this Thread