0

I got this pseudocode from Wikipedia:

procedure bubbleSort( A : list of sortable items )
   n = length(A)
   repeat 
     swapped = false
     for i = 1 to n-1 inclusive do
       /* if this pair is out of order */
       if A[i-1] > A[i] then
         /* swap them and remember something changed */
         swap( A[i-1], A[i] )
         swapped = true
       end if
     end for
   until not swapped
end procedure

And this from a book (named Principles of Computer Science)

BubbleSort( list )
    length  <-- lenght of list
    do  {
        swapped_pair    <-- false
        index       <-- 1
        while index <= length - 1 {
            if list[index] > list[index + 1] {
                swap( list[index], list[index + 1] )
                swapped_pair = true
                index <-- index + 1
            }
        }
    } while( swapped = true )
end

I don't know which is better pseudocode.

The parts I don't understand is the swapped_pair <-- false part and the last lines.

In the line 4 when it's written swapped=false or swapped_pair <-- false.

Why it's set to false at the start? What would happen if it weren't set to false?

And the last lines, on the Wikipedia it's written:

       end if
     end for
   until not swapped
end procedure

And on the pseudocode from the book it's written:

while( swapped = true )

What does these last lines mean?

Coder88
  • 1,015
  • 3
  • 10
  • 23
  • There are two loops. Where do you get the idea that it's only going through the list once? – Oliver Charlesworth Sep 17 '15 at 22:33
  • Also, it sounds like you're having trouble understanding the syntax of the pseudocode, rather than the algorithm. I suggest you review the chapter of the book where the syntax is introduced. – Oliver Charlesworth Sep 17 '15 at 22:35
  • Excuse me, I didn't got that idea really (I'll edit it). But I just don't understand what the last statements mean. And without the last statements, if I'm not wrong, that would go through the list once. – Coder88 Sep 17 '15 at 22:39
  • I know the bubble-sort algorithm, but I didn't understand how it's executed on these pseudocodes. – Coder88 Sep 17 '15 at 22:47
  • The pseudo code from the book doesn't end with `while( swapped = true )` but rather it ends with `} } } while( swapped = true ) end` so that makes them the same. In the book they use `}` to close an if or loop and `end` to end the function but in wikipedia they use `end if` and `end for` and `end procedure` for the same thing - so really it is exactly the same with different words for the same thing. And `<--` is really just `=` so `swapped=false` and `swapped_pair <-- false` are the same thing as well. – Jerry Jeremiah Sep 17 '15 at 22:48
  • So `repeat . . . until not swapped` is same as `do {. . .} while( swapped = true )` ? But what does "swapped" mean in this code (and why the procedure continues until swapped=false?) – Coder88 Sep 17 '15 at 22:58

1 Answers1

1

The swapped variable keeps track if any swaps were made in the last pass through the array.

  • If a swap was made, the array is still not sorted and we need to continue.
  • If no swaps were made, then the array is already sorted and we can stop there. Otherwise we will do redundant iterations.

This is one of the optimizations that we ca do to make bubble sort more efficient. If you are interested in more optimizations you can look here: http://www.c-programming-simple-steps.com/bubble-sort.html

However, even optimized, bubble sort is too inefficient to be used in practice. It is an interesting case to look at, while learning, but if you need a simple sort algorithm use insertion sort instead.

M.Paunov
  • 1,737
  • 1
  • 15
  • 19