Loop is within loop for

3
for(pass = 0; pass < size - 1; pass++){
    for(j = 0; j < size - 1; j++){

        if(array[j] > array[j + 1]){
            swap( &array[j], &array[j + 1]);
         }
     }
 }

I just put a piece of code where I have a doubt, but the intention is to sort the numbers in a array of size 10. The part I do not understand is the utility of the second < in> loop for , inside the loop is external. given to array .

a = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37}

Only one loop would be enough to put in ascending order, but why is another loop

The exercise has the following comment in this loops :

1° loop = loop to control passes
2° loop = loop to control comparisons during each pass.
    
asked by anonymous 18.01.2017 / 03:08

1 answer

4

This is more a matter of logic and mathematics.

It even has different ways of doing this, but in this example you need two loops because the internal only compares one pair at a time and reverses if the second is the smallest, but this does not guarantee changes other than the pair. The external loop will cause you to do other times to ensure that you analyze it again and bring the smaller ones closer and closer to the beginning >

Let's take a table test:

2, 6, 4, 8, 10, 12, 89, 68, 45, 37

Compara 2 e 6 e não faz nada
Compara 6 e 4 e inverte 2, 4, 6, 8, 10, 12, 89, 68, 45, 37
Compara 6 e 8 e não faz nada
Compara 8 e 10 e não faz nada
Compara 10 e 12 e não faz nada
Compara 12 e 89 e não faz nada
Compara 89 e 68 e inverte 2, 4, 6, 8, 10, 12, 68, 89, 45, 37
Compara 89 e 45 e inverte 2, 4, 6, 8, 10, 12, 68, 45, 89, 37
Compara 89 e 37 e inverte 2, 4, 6, 8, 10, 12, 68, 45, 37, 89

Is it tidy? No. Improved, but still need to sort more:

2, 4, 6, 8, 10, 12, 68, 45, 37, 89

Compara 2 e 4 e não faz nada
Compara 4 e 6 e não faz nada
Compara 6 e 8 e não faz nada
Compara 8 e 10 e não faz nada
Compara 10 e 12 e não faz nada
Compara 12 e 68 e não faz nada
Compara 68 e 45 e inverte 2, 4, 6, 8, 10, 12, 45, 68, 37, 89
Compara 68 e 37 e inverte 2, 4, 6, 8, 10, 12, 45, 37, 68, 89
Compara 89 e 37 e inverte 2, 4, 6, 8, 10, 12, 68, 45, 37, 89

Is it tidy? No. It has improved, but you still have to order more and so you have to do it again until you have everything sorted out.

Not the most efficient, but this code works like this. As already said in the comments it is possible to mark when it is already ordered and not to do all steps. In fact it is possible not to make the exchanges where it is already ok. But there's another quite different algorithm.

Let's see how it goes:

#include <stdio.h>

void swap(int *e1, int *e2) {
    int tmp = *e1;
    *e1 = *e2;
    *e2 = tmp;
}

void imprime(int array[], int size) {
    for (int j = 0; j < size; j++) {
         printf("%d, ",array[j]);
     }
     printf("\n");
}

int main(void) {
    int array[10] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
    int size = 10;
    for (int pass = 0; pass < size - 1; pass++) {
        for (int j = 0; j < size - 1; j++) {
            if (array[j] > array[j + 1]) {
                swap(&array[j], &array[j + 1]);
             }
             imprime(array, size);
         }
         printf("--- Nova iteração ---\n");
     }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
18.01.2017 / 03:56