1

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.

mrflash818
  • 930
  • 13
  • 24

2 Answers2

0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

What you want to achieve is some sort of a modified insertion sort. Check out this implementation :

void sort(int values[], int n)
{
    for (int i = 0; i < n; ++i)
    {
        int min = i;
        for ( int j = i ; j < n ; j++)
        {
            if (values[j]<values[min]) min =j;
        }
        while(values[min-1]>values[min])
        {
            if(min==0)
                break;
            if(values[min-1]!=values[min])
                swap (values+min-1,values+min);
            min--;

        }
    }
    return;
}

What this function does is that it searches the array every time for the smallest element starting from index i and when it finds this element it keeps on swapping it will all of the previous elements in the array until it reaches a smaller element.

Ahmed Karaman
  • 523
  • 3
  • 12