Selection Sort in C++ I wrote a little C++ sorting routine for my Algorithm studies, but I feel that a lot can be skipped out.

I am aware that modern sort takes Iterators and comparator functions, let's disregard that for now. I just want to clean up this code. Specifically, I don't like the if and value variables.

I have looked into other solutions, but they didn't clear up much for me.

void SelectionSorter::sort(std::vector&lt;int&gt; &amp;v){

    int currentIndex = 0;

    while (currentIndex &lt; v.size()) {

        int indexOfSmallestItem = -1;
        int value = v[currentIndex];
        bool shouldSwap = false;

        for (int i = currentIndex; i &lt; v.size(); i++) {
            if (v[i] &lt; value) {
                value = v[i];
                indexOfSmallestItem = i;
                shouldSwap = true;
            }
        }

        if (shouldSwap) {
            int temp = v[currentIndex];
            v[currentIndex] = v[indexOfSmallestItem];
            v[indexOfSmallestItem] = temp;
        }

        currentIndex++;
    }
}
Some points: 


The shouldSwap variable is not needed. Instead, set indexOfSmallestItem to currentIndex at the beginning of the loop (this is necessary for the next step to work) and check is the indexOfSmallestItem is currentIndex:

if (indexOfSmallestItem != currentIndex) {
    int temp = v[currentIndex];
    v[currentIndex] = v[indexOfSmallestItem];
    v[indexOfSmallestItem] = temp;
}

value is also not needed, as you can access the vector directly:

    if (v[i] &lt; v[indexOfSmallestItem]) {
        value = v[i];
        indexOfSmallestItem = i;
    }

The outside while loop can be a for loop, because:

int currentIndex = 0; // initialization

while (currentIndex &lt; v.size()) { // condition

    // ...

    currentIndex++; // increment
}


So it would be:

for (int currentIndex = 0; currentIndex &lt; v.size(); currentIndex++) {
    // ...
}

You actually can start on currentIndex + 1 because the first one is the current value.
The naming of the vector is not good, as one-letter names are hard to read. Change it to vect or something similar.


Now all you need is the indexOfSmallestItem and the vector:

void SelectionSorter::sort(std::vector&lt;int&gt; &amp;vect) {

    for (int currentIndex = 0; currentIndex &lt; vect.size(); currentIndex++) {

        int indexOfSmallestItem = currentIndex;

        for (int i = currentIndex + 1; i &lt; vect.size(); i++) {
            if (vect[i] &lt; vect[indexOfSmallestItem]) {
                indexOfSmallestItem = i;
            }
        }

        if (indexOfSmallestItem != currentIndex) {
            int temp = vect[currentIndex];
            vect[currentIndex] = vect[indexOfSmallestItem];
            vect[indexOfSmallestItem] = temp;
        }

    }

}


Optionally, you can remove the if (indexOfSmallestItem != currentIndex) completely, as it is not much of an improvement.

Also, you can extract the swapping part into a method. In fact, you should. This makes code (even this size) easier to maintain.

void SelectionSorter::sort(std::vector&lt;int&gt; &amp;vect) {
    for (int currentIndex = 0; currentIndex &lt; vect.size(); currentIndex++) {
        int indexOfSmallestItem = currentIndex;
        for (int i = currentIndex + 1; i &lt; vect.size(); i++) {
            if (vect[i] &lt; vect[indexOfSmallestItem]) {
                indexOfSmallestItem = i;
            }
        }
        swap(vect, currentIndex, indexOfSmallestItem);
    }
}

void swap(std::vector&lt;int&gt; &amp;vect, int i, int j) {
    int temp = vect[i];
    vect[i] = vect[j];
    vect[j] = temp;
}