# Merge and sort array.

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 01-30-2013
Quote:

Originally Posted by c99tutorial
That is a sorting algorithm. To see how it works, write it out in terms of "pseudocode" and then try it out with pencil and paper

Code:

```for i = 0...length-1   for j = 0...length-1-i       if data[i] > data[j+1]:         swap data[i] and data[j+1]       end   end end```
On paper you can draw i and j as two different arrows that point to one of the array items, each one marked with a number 0, 1, 2, ...., length-1

I wouldn't recommend this ^^^^ algorithm. That's a Substitution sort with an offset of 1, and also goes out of bounds (off the top) of the array, because of the +1.

I'm sure you meant data[i] > data[i+1], for the comparison and the swap.

If you aren't comparing adjacent elements, it's not a Bubble sort - and it won't b-u-b-b-l-e. (Oh! The horror! LOL ).
• 01-30-2013
std10093
I am posting again the pseudocode here, in order to avoid confusion.