1. ## sorting four numbers

I'm trying to take a users input and break it up into four separate numbers, then take those numbers and arrange them from smallest to largest.

So far I can't seem to get them working right, and i'm running out of ideas if anyone could give me an idea, or even a nudge in the right direction, I would greatly appreciate it.

Code:
```# include <stdio.h>main ()
{
int inputVariables[4]; //where userinput goes after being broken up
int arrangedValues [4];// the user values arranged lowest to highest
int i;
int j;
int userInput;
int remainders; //used to break up input
int numberX; // users input arranged forwards
int numberY; // users input arranged forwards

printf ("Please state a number from 1000 - 9999, excluding numbers with four identical digits i.e 9999 \n");
scanf (" %d",&userInput);

remainders = userInput % 1000;
inputVariables[0] = userInput / 1000;
userInput = remainders;

remainders = userInput % 100;
inputVariables[1] = userInput / 100;
userInput = remainders;

remainders =userInput % 10;
inputVariables[2] = userInput / 10;
userInput = remainders;
inputVariables[3] = remainders;

printf ("%d \n",inputVariables[0]);
printf ("%d \n",inputVariables[1]);
printf ("%d \n",inputVariables[2]);
printf ("%d \n",inputVariables[3]);
printf ("%d \n", remainders);

do
{

for (i = 0 ; i < 4; i++)
{
for (j = 0 ; j < 4; j++)
{
if (inputVariables[i] < inputVariables[j])
{
arrangedValues[i] = inputVariables[j];
}

}
}
}
while (arrangedValues[0] >= arrangedValues[1] >= arrangedValues[2] >= arrangedValues[3]);

printf(" %d %d %d %d \n",arrangedValues [0],arrangedValues [1],arrangedValues [2],arrangedValues [3]);
printf(" %d %d %d %d \n",arrangedValues [3],arrangedValues [2],arrangedValues [1],arrangedValues [0]);

}```

2. I've been searching online and I found this code and it looks like it does what I want, and that I am not too far off, but I just can't wrap my head around how it works.

I went through it using a pen and paper (using the numbers 5 2 7, to walk through it) and I get to seven and I just can't wrap my head around what happens next.

Code:
```void BubbleSort(int a[], int array_size)
{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}```

if someone could at least explain how this works I can understand and adapt my own coding.

3. Originally Posted by Fear_Impurity
if someone could at least explain how this works I can understand and adapt my own coding.
There's a distinct reason it's called "Bubble sort". While there are values with higher index and lower value ( or higher value, depending on the sorting order ), exchange them. This results in an extremely slow but rather trivial sort algorithm where, if each value is seen as a bubble, it rises slowly to its proper sorted place.

4. Originally Posted by Fear_Impurity
I've been searching online and I found this code and it looks like it does what I want, and that I am not too far off, but I just can't wrap my head around how it works.

I went through it using a pen and paper (using the numbers 5 2 7, to walk through it) and I get to seven and I just can't wrap my head around what happens next.

Code:
```void BubbleSort(int a[], int array_size)
{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}```

if someone could at least explain how this works I can understand and adapt my own coding.
I understand how the bubble sort concept works, but why does it require two loops, from going over the code, everything seems to happen only in the second loop.

5. Run it with only the second loop to see what happen. Try more than one inputs.