Bookmark Topic Watch Topic
  • New Topic

Cant understand sorting loop in sorting algorithm.  RSS feed

 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Report post to moderator
I kept trying for days to code the non-recursive merge sort algorithm and couldn't do so correctly! I now came to the point where I began to debug and try to understand what is going on in an example I found online (which sorts an array non-recursively). I would greatly appreciate if anyone could explain to me what is going on through out this code. Especially in this loop:



The rest of the code is understandable but here is where I am getting confused in the logic. I wouldn't mind if you explained the rest of the code, but this is the major part I don't understand.

All code:

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please format and indent the code correctly; we have some suggestions here.
Don't declare two variables on the same line.
Your if‑elses should read like this:-Otherwise it is difficult to read.
Please tell us where that logic comes from.
Please write it out on paper, with some examples of arrays; get your computer to print a few ints chosen randomly with this method, or similar, write them down, and go through the sorting algorithm with a pencil. Then you can see it working. Alternatively, Matthew Brown gave you a link which explains the sorting algorithms. What did you learn from that?

What do you mean about mid >= a.length? How can you get a mid which is greater than the lengths of the arrays?

I think you are starting too many new threads, and your questions are all about the same thing, so I am closing this thread. Please continue all discussion here.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!