Sort query - Language C

3

I'm sorting a simple, growing vector. The code works.

But the following line in which left me confused:

if (matriz[i] < matriz[j])

Why is the sign there smaller, not larger? If I want to change the positions, if the i position is greater than the j position, why is the sign against it?

#include "stdio.h"
#include "stdlib.h"
#include "time.h"

int main()
{
    int k = NULL, *matriz = NULL, aux=0;    

    printf("Tamanho do vetor: ");
    scanf("%i", &k);

    matriz = (int *)malloc(k * sizeof(int));
    srand(time(0));

    printf("\n");
    printf("NAO ORDENADO: ");
    printf("\n");
    for (int i = 0; i < k; i++)
    {

        matriz[i] = rand() % 100;

        printf("Posicao %d: %d", i + 1, matriz[i]);
        printf("\n");
    }

    for (int i = 0; i < k; i++)
    {
        for (int j = 0; j < k; j++)
        {
            if (matriz[i] < matriz[j])
            {
                aux = matriz[i];
                matriz[i] = matriz[j];
                matriz[j] = aux;
            }
        }
    }

    printf("\n");
    printf("ORDENADO: ");
    printf("\n");
    for (int i = 0; i < k; i++)
    {
        printf("Posicao %d: %d", i + 1, matriz[i]);
        printf("\n");
    }

    printf("\n");
    printf("\n");

    system("pause");
    return 0;
}
    
asked by anonymous 27.04.2015 / 21:34

2 answers

1

Let's assume the following vector: [2,5,1,6] . I'll do step by step how your code will handle this vector to see if it gets any clearer.

> 2 < 2? Não
[2,5,1,6]
> 2 < 5? Sim
[5,2,1,6] Como o dois é menor, ele troca de lugar com o cinco.
> 5 < 1? Não
[5,2,1,6]
> 5 < 6? Sim
[6,2,1,5]

Remember that at this point you "end" the first loop of the for off, so let's start comparing the second vector item with the others:

> 2 < 6? Sim
[2,6,1,5]
[2,6,1,5]
> 6 < 1? Não
[2,6,1,5]
> 6 < 5? Não
[2,6,1,5]

> 1 < 2? Sim
[1,6,2,5]
> 2 < 6? Sim
[1,2,6,5]
> 6 < 6? Não
[1,2,6,5]
> 6 < 5? Não
[1,2,6,5]

> 5 < 1? Não
[1,2,6,5]
> 5 < 2? Não
[1,2,6,5]
> 5 < 6? Sim
[1,2,5,6]
>6 < 6? Não

I hope you have understood.

    
27.04.2015 / 22:01
1

I think its confusing is that the algorithm used is unintuitive.

For the comparison to be as you say the algorithm should look like this:

Each element of array for (int i = 0; i < k - 1; i++) is compared to the following for (int j = i + 1; j < k; j++) )

As the ordering is increasing, if the element is greater than the next it should be changed if(matriz[i] > matriz[j])
Where i is the position of the element in question and j is the position of the element that is in a next position.

The last element does not need to be checked because the antepenultimate check ensures that it is the largest of all.

In your code you should change the two cycles to:

for (int i = 0; i < k - 1; i++)
{
    for (int j = i + 1; j < k; j++)
    {
        if (matriz[i] > matriz[j])
        {
            aux = matriz[i];
            matriz[i] = matriz[j];
            matriz[j] = aux;
        }
    }
}

This algorithm is faster, as fewer iterations are required to get the result.
See the Ideone

    
27.04.2015 / 23:11