Problems when printing the sorted number file

Asked

Viewed 62 times

2

Guys, good night, I’m having a problem with this algorithm, I ask you to enlighten me with your knowledge.
I apologize in advance if it’s hard to understand the code, I’m still in second period.

The goal of the algorithm is:

Generate an array with n random numbers and save them in a file, sort these numbers according to the option given in a procedure that asks the type of sorting desired, and finally print the vector numbers in another file, only this time properly ordered.

The problem is:

When printing the files with ordered numbers, where one file should contain the random numbers and the other file the same numbers, only ordered, but they are not ordering.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>

#define TAM 5000

void opcoes();
void CV(int *n);                                            //Criar vetor                                                 
void OV(int *n, int opcao, int ini, int fim);               //Ordenar vetor         
int PV(int *n, int ini, int fim);                           //Particionar vetor         
void IV(int *n);                                            //Imprimir vetor            

int main(){
    srand(time(0));
    setlocale(LC_ALL,"Portuguese");

    int t, i, n[TAM], ini=i, fim=TAM-1;

    opcoes();
    t=clock();
    CV(n);
    OV(n,0,ini,fim);
    IV(n);
    t=clock()-t;

    printf("\nTempo de processamento: %0.3f segundos.\n",(float)(t)/CLOCKS_PER_SEC);
    return 0;
}

Procedure for selecting a particular type of sorting:

void opcoes(){
    int opcao;

    do{
    printf("\nDigite a opção do algoritmo de ordenação: \n\n1 - BUBBLE SORT\n2 - INSERTION SORT\n3 - SELECTION SORT\n4 - QUICK SORT\n\n");
    scanf("%d",&opcao);
    }while((opcao<1)||(opcao>4));
}

Procedure for creating the vector:

 void CV(int *n){
    int i; FILE *A;

    A=fopen("A.txt","r");
    if(A==NULL){
        printf("\n\"A.txt\" inexistente, criando arquivo.\n");
        fclose(A);

            A=fopen("A.txt","w");
            for(i=0;i<TAM;i++){
                n[i]=rand()%10000+1;
                //printf("%d ",n[i]);
                fprintf(A,"%d ",n[i]);
            }
    fclose(A);
    }
fclose(A);
}

Procedure to sort the vector according to the option given in the procedure options:

void OV(int *n, int opcao, int ini, int fim){
    int i, j, pivo, sel, aux;

    switch(opcao){

        case 1:                                     //bubble sort
            for(i=0;i<TAM;i++){
                for(j=0;j<TAM-1;j++){
                    if(n[j]<n[j+1]){
                        aux=n[j];
                        n[j]=n[j+1];
                        n[j+1]=aux;
                    }
                }
            }
        break;

        case 2:                                     //insertion sort
            for(i=1;i<TAM;i++){
                aux=n[i];
                for(j=i-1;(j>=0)&&(aux>n[j]);j--){
                    n[j+1]=n[j];
                }
                n[j+1]=aux;
            }
        break;

        case 3:                                     //selection sort
            for(i=0;i<TAM-1;i++){
                sel=i;
                for(j=i+1;j<TAM;j++){
                    if(n[j]<n[sel]){
                    sel=j;
                    }
                }
                if(i!=sel){
                    aux=n[i];
                    n[i]=n[sel];
                    n[sel]=aux;
                }
            }
        break;

        case 4:                                     //quick sort
            if(ini<fim){
                pivo=PV(n,ini,fim);
                OV(n, opcao, ini, pivo-1);
                OV(n, opcao, pivo+1,fim);
            }
        break;  
    }
}

Function partitioning the vector in the case of a Bubble Sort:

int PV(int *n, int ini, int fim){
    int pivo=n[ini], i=ini,  j, aux;

    for(j=ini+1;j<=fim;j++){
        if(n[j]>=pivo){
            i++;
            aux=n[i];
            n[i]=n[j];
            n[j]=aux;
        }
    }
    aux=n[i];
    n[i]=n[ini];
    n[ini]=aux;

    return i;
}

Procedure for printing the vector (in ordered thesis) in another file.

void IV(int *n){
    int i; FILE *OA;

    OA=fopen("Ordenado-A.txt","r");
    if(OA==NULL){
        printf("\n\"Ordenado-A.txt\" inexistente, criando arquivo.\n");
        fclose(OA);

        OA=fopen("Ordenado-A.txt","w");
        for(i=0;i<TAM;i++){
            fprintf(OA,"%d ",n[i]);
            //printf("%d ",n[i]);
        }
        fclose(OA);
    }
    fclose(OA);
}

1 answer

0

In calling the function OV, you pass zero in the second argument.

int main(){
srand(time(0));
setlocale(LC_ALL,"Portuguese");

int t, i, n[TAM], ini=i, fim=TAM-1;

opcoes();
t=clock();
CV(n);
OV(n,0,ini,fim);
IV(n);
t=clock()-t;

printf("\nTempo de processamento: %0.3f segundos.\n",(float)(t)/CLOCKS_PER_SEC);
return 0;
}

This value is processed by the switch within the function and does not operate any action, because the value zero is not included in any of the cases.

What can be done is this:

int main(){
srand(time(NULL));
setlocale(LC_ALL,"Portuguese");

int t, i, n[TAM], ini=i, fim=TAM-1;

int opcao = opcoes();
t=clock();
CV(n);
OV(n,opcao,ini,fim);
IV(n);
t=clock()-t;

printf("\nTempo de processamento: %0.3f segundos.\n",(float)(t)/CLOCKS_PER_SEC);
return 0;
}
  • It worked, now just need to put the vector to be ordered.

Browser other questions tagged

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