title: Intro to C++: Sorting code
body: <p>I am studying for a final exam that is scheduled today. My question is, what does the do-while(swap) statement do here? Is swap set to "true" by default? </p>
  
  <p>My understanding of this code is that it iterates at least once for each pass of the sort. The inner for loop carries the crucial code and does all the swaps in the pass. For example, array[count] is copied into temp, array[count+1] is copied into array[count] and array[count] is copied into array[count+1]. If the two elements are swapped, the bool swap is set to true. The outer loop keeps iterating until it finds the swap flag false. </p>
  
  <pre><code>void sortArray(int array[], int size)
  {
     int size;
     bool swap;
  
  do
  {
     swap = false;
     for(int count = 0; count &lt; (size -1); count++)
     {
        if(array[count] &gt; array[count+1])
        {
           temp = array[count];
           array[count] = array[count+1];
           array[count+1] = temp;
           swap = true;
         }
      }
    }while(swap);
   }
  </code></pre>