Sorting Doubt - C Language

Asked

Viewed 129 times

3

I am ordering a simple vector, increasingly. The code works.

But the next line that got me confused:

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

Why is the sign there smaller, not bigger? If I want to change positions, if the position i is greater than the position j, because the sign is contrary to this?

#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;
}
  • It calls it vector: a unidimencional matrix is like having a dog called cat :)

2 answers

1

Let’s assume the following vector: [2,5,1,6]. I’ll do step by step how your code will treat this vector to see if it becomes 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 in this moment "ends" the first loop of for from the outside, 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’ve made your point.

1

I believe that its confusion lies in the fact that the algorithm used is not very intuitive.

For the comparison to be as you say the algorithm should be so:

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

As the ordination is increasing, if the element is greater than the following should be exchanged if(matriz[i] > matriz[j])
Where i is the position of the element in question and j is the position of the element which 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 on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.