2.2. Invoking a Function
In this section we implement a function to sort a vector of integer values so that we can explore the behavior of passing parameters both by reference and by value. The sorting algorithm is a simple bubble sort implemented by two nested for loops. The outer for loop walks through the vector elements from ix, which begins at 0 and ends at size-1. The idea is that upon completion of each iteration of the outer loop, the element indexed by ix is in its proper place. When ix is 0, the smallest element is found and placed at position 0, and when ix is 1, the second smallest element is in place, and so on. The placement is executed by the inner for loop. jx begins at ix+1 and ends at size-1. It compares the element value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access