C:冒泡排序没有完成交换

时间:2015-03-24 09:07:29

标签: c bubble-sort

我的程序应该从文件中对数据进行排序。但是输出显示它还没有完成排序,如下所示:

123 DoeJohn 512

121 SmithSam 1022

163 BeamJim 2023

183 DanielsJack 2932

323 BaileyJim 922

0 0 0

我很确定我的冒泡排序功能是正确的。但我不知道为什么它会在它完成之前停止交换。

输出结束时出现第二个问题为零。它应该只有5行数据。请指教?提前谢谢。

#include <stdio.h>
#include <string.h>
#define MAX 100

/* Define Structure */
/* ---------------- */
struct emp
{
   int      id_num;     /* employee number */
   float    salary;     /* employee salary */
   char first_name[20]; /* employee first name */
   char last_name[30];  /* employee last name */
};

void sortit (struct emp*, int);

main () 
{


    /* Declare variables */
    /* ----------------- */
    struct emp info[100];   /* a maximum 100 people can be stored */
    FILE    *in_file_ptr, *out_file_ptr;
    int     i, count;
    char    filename[30];
    char    sort_by;

    /* Greetings */
    /* --------- */
    printf ("Welcome to Employee Center.\n");

    /* Prompt user for file name */
    /* ------------------------- */
    printf ("\nEnter file name: ");
    scanf ("%s", filename);
    fflush(stdin);


    /* Open the input file. If error, display message and exit  */
    /* --------------------------------------------------------------- */
    in_file_ptr = fopen(filename, "rb");
    if (!in_file_ptr)
    {
        printf ("\nCannot open file %s for reading.\n", filename);
        return 1;
    }


    for ( i = 0; i < 100; i++ )
    {
        /* Read data from input file and load array struct */
        /* ------------------------------------------------ */
        fread (&info[i], sizeof(info[i]), 1, in_file_ptr);


        sortit (info, count);

        /* Concatenate first name and last name string */
        /* ------------------------------------------ */    
        strcat  (info[i].last_name, info[i].first_name);

        printf ("%10i %20s %-10.2f\n", info[i].id_num, 
            info[i].last_name, info[i].salary);


        if(feof(in_file_ptr))break;

    } /* end for loop */

    fclose (in_file_ptr);

} /*end main */

void sortit (struct emp a[], int num)
/* takes a single dim array of int and sorts the array in place */
{
    int j, i, temp;


    /* This sorts the array - bubble sort */

    for ( j=0; j<num; j++ )
        for ( i=0; i<=num-1; i++ )
            if ( a[i].id_num > a[i+1].id_num )
            {
                temp = a[i].id_num;
                a[i].id_num = a[i+1].id_num;
                a[i+1].id_num = temp;   

            } /* end if */


} /* end sortit function */enter code here

3 个答案:

答案 0 :(得分:0)

sortit (struct emp a[], int num)函数中更改第二个for()循环

for ( i=0; i<=num-1; i++ )

for ( i=0; i<num-1; i++ )

答案 1 :(得分:0)

#include <stdio.h>
#include <string.h>
#define MAX 100
/* Define Structure */
struct emp{
        int id_num;     /* employee number */
        float salary;     /* employee salary */
        char first_name[20]; /* employee first name */
        char last_name[30];  /* employee last name */
}

void sortit (struct emp*, int);
main(){
       /* Declare variables */
       /* ----------------- */
       struct emp info[MAX];   /* a maximum 100 people can be stored */
       FILE    *in_file_ptr, *out_file_ptr;
       int     i, count;
       char    filename[30];
       char    sort_by;
       /* Greetings */
       /* -------- */
       printf ("Welcome to Employee Center.\n");

      /* Prompt user for file name */
      /* ------------------------- */
      printf ("\nEnter file name: ");
      scanf ("%s", filename);
      fflush(stdin);
      /* Open the input file. If error, display message and exit  */
      /* ----------------------------------------------------------- */
      in_file_ptr = fopen(filename, "rb");
      if (!in_file_ptr)
      {
             printf ("\nCannot open file %s for reading.\n", filename);
             return 1;
      }
      for ( i = 0; i < 100; i++ )
      {
            /* Read data from input file and load array struct */
            /* ------------------------------------------------ */
            fread (&info[i], sizeof(info[i]), 1, in_file_ptr);


            /* Concatenate first name and last name string */
            /* ------------------------------------------ */    
            strcat  (info[i].last_name, info[i].first_name);

            printf ("%10i %20s %-10.2f\n", info[i].id_num, info[i].last_name, info[i].salary);

            if(feof(in_file_ptr))break;

      } /* end for loop */

      sortit (info, count);

      **/*---Put a Code to View sorted List Here.---*/**

      fclose (in_file_ptr);

} /*end main */
/* takes a single dim array of int and sorts the array in place */
void sortit (struct emp a[], int num){
      int j, i, temp;

      /* This sorts the array - bubble sort */

      for ( j=0; j<num; j++ )
           for ( i=0; i<num-1; i++ )
               if ( a[i].id_num > a[i+1].id_num )
               {
                      temp = a[i].id_num;
                      a[i].id_num = a[i+1].id_num;
                      a[i+1].id_num = temp;   

                } /* end if */
} /* end sortit function */

答案 2 :(得分:0)

 if ( a[i].id_num > a[i+1].id_num ) 

应该是:

 if ( a[j].id_num > a[i+1].id_num )