Q:

Sorting through array c++

So I am attempting to do a bubble-like sort. Not a bubble sort because I don't want to exchange every single value that i run into. I simply want to find the smallest value of each index and place it in order. Such as in the arrayVal[3, 5, 2].
Instead of replacing the value 3 with 2 and then replacing 5 with 3. I want to find the smallest number of the entire array and place it at arrayVal[0] and then move to arrayVal[1]. 
I can't quite figure out how to do this and am kind of stuck.(I took out the <> on the libraries so you could see which libraries I am using)
#include iostream
#include cmath
#include ctime
using namespace std;

int main()
{
    const int STARTLOOP = 0;
    const int MAXLOOP = 5;
    const int MINRANGE = 1;
    const int MAXRANGE = 10;

    //int temp = 0;
    int smallestVal = 0;
    int arrayVal[MAXLOOP];

    srand(time(0));

    for (int i = STARTLOOP; i < MAXLOOP; i++)
    {
        arrayVal[i] = (rand() % MAXRANGE) + MINRANGE;
    }

    for (int i = STARTLOOP; i < MAXLOOP; i++)
    {
        cout << arrayVal[i] << endl;
    }

    cout << "Before the sort" << endl;

    for (int i = STARTLOOP; i < MAXLOOP; i++)
    {
        for (int j = i; j < MAXLOOP; j++)
        {
            if (arrayVal[j] < arrayVal[i])
            {
                arrayVal[i] = smallestVal;
            }

        }
    }

    for (int i = STARTLOOP; i < MAXLOOP; i++)
    {
        cout << arrayVal[i] << endl;
    }

    cout << "After the sort" << endl;

    return 0;

I also recognize I'm not using functions, i just wrote up the code because I was trying to figure this out. Thank you in advance.

A:

You're never setting smallestVal to anything from the array. You don't need nested loops. Just go through the array once, comparing each value to smallestVal. If it's smaller, you set smallestVal to that value. You should also have a variable that holds the index of the smallest value, which you update at the same time.
At the end, you swap the first element with the smallest one.
int smallestVal = arrayVal[STARTLOOP];
int smallestIndex = STARTLOOP;
for (int i = STARTLOOP + 1; i < MAXLOOP; i++) {
    if (arrayVal[i] < smallestVal) {
        smallestVal = arrayVal[i];
        smallestIndex = i;
    }
}
if (smallestIndex != STARTLOOP) { 
    // swap it with the first element
    int temp = arrayVal[STARTLOOP];
    arrayVal[STARTLOOP] = smallestVal;
    arrayVal[smallestIndex] = temp;
}

You can then increment STARTLOOP and repeat this.

