Problems when printing the file of ordered numbers

2

Well, good night, I'm having a problem with this algorithm, so please enlighten me with your knowledge. I apologize in advance if it is difficult to understand the code, I am still in the second period.

  

The goal of the algorithm is:

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

  

The problem is:

At the time of printing files with sorted numbers, where one file should contain the random numbers and the other file the same numbers, only ordered but they are not sorting.

#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 to select a particular sort order:

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 to create 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 Options procedure:

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 that partitions the vector in 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 to print the vector (in an 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);
}
    
asked by anonymous 05.08.2018 / 23:34

1 answer

0

When calling the OV function, 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 perform any action, since the value zero is not included in any of the cases.

What can be done is as follows:

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;
}
    
07.08.2018 / 13:08