1. ## Can someone please explain this array sorting program?

Code:
```#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>

int main()
{
int i,j;
double a[9];

printf ("This program determines the middle value\n");
printf ("of nine real numbers that you enter from the keyboard\n");

for (i = 0; i < 9; i++)
{
printf ("%d: ", i);
scanf("%lg", &a[i]);
}

// Print the numbers
printf ("You entered:\n");
for (i = 0; i < 9; i++)
{
printf ("%d:  %lg\n", i, a[i]);
}
printf ("\n\n");

// Sort the array
for (i = 0; i < 8; i++)
{
for (j = i+1; j < 9; j++)
{
if (a[j] < a[i])
{
// Exchange them
double temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}

printf ("Sorted array:\n");
for (i = 0; i < 9; i++)
{
printf ("%d:  %lg\n", i, a[i]);
}
printf ("\n\n");

printf ("The middle value is %lg\n", a[4]);
getchar();
getchar();
return 0;
}```
I don't understand the part about sorting the array. I know I am misunderstanding it, but I have been trying to understand it for quite some time and its not making sense to me.

For example: Say these are my array values
a[0]=5
a[1]=3
a[2]=2
a[3]=1
a[4]=7
a[5]=8
a[6]=9
a[7]=2
a[8]=3

This is how I am interpreting the program:

Is a[1] < a[0]? If so, switch them.

Since a[0] = 5 and a[1] = 3 this is true. Thus, I switch them and get:

a[0] = 3 and a[1] = 5.

Then i and j are both incremented to get the following:

is a[2] < a [1]? If so switch them.

a[1] is now 5, correct? Thus, a[1] IS larger than a[2]. So we switch them.

a[1] = 2 and a[2] = 5.

I continued, but there is already a problem here.

So far the array is a[0]=3, a[1] = 2, and a[2] = 5.

This is not sorted already from low to high. How exactly is this program taking values from a[8] for example and comparing them with a[2]?

I am just having trouble understanding this. I apologize for the long post, but I would really appreciate it if anyone can tell me exactly whats going on.

Thanks!
-Frankie

2. They aren't both incremented:
Code:
```    for (i = 0; i < 8; i++)
{
for (j = i+1; j < 9; j++)
{
if (a[j] < a[i])
{
// Exchange them
double temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}```
First pass:
Code:
```if a1 < a0
swap a1, a0```
Second pass of inner loop:
Code:
```if a2 < a0
swap a2, a0```
...
Code:
```if a8 < a0
swap a8, a0```
Outer loop increments. Inner loop starts over:
Code:
```if( a2 < a1 )
swap a2, a1```
Repeat until done.

Quzah.

3. Inside the sort you are declaring double temp ... move the declaration to the top of main and jsut use temp = inside the sort.

Also... your outer loop ends at 8, but it needs to run the full array, the inner loop should be limited to only unsorted values.

4. Originally Posted by Frankie15
Then i and j are both incremented to get the following:
That line seems to be the source of your misunderstanding. The j loop is nested inside the i loop. That means j increments from i+1 all the way to 8, then i increments, then j resets to the new i+1, and goes all the way up to 8 again before i increments again. A table of values for i and j:
Code:
```i j
- -
0 1
0 2
0 3
...
0 8
1 2
1 3
...
1 8
2 3
...
7 8```

5. Thanks for the tables quzah and anduril. I think I understand it. It does much more than I originally thought it did.

Really appreciate the quick responses. You guys were very helpful!