微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将合并排序接受数组转换为结构数组

如何解决如何将合并排序接受数组转换为结构数组

 #include <stdio.h>
 #include <stdlib.h>   
  struct Employ
 {
int id,age;
char name[100],desg[100];
char sec;
};
void mergeSort(struct Empoly emp,int low,int mid,int high)
{
int i,j,k,l,b[50];

l = low;
i = low;
j = mid + 1;
while ((l <= mid) && (j <= high))
{
    if (a[l] <= emp[j])
    {
        b[i] = emp[l];
        L++;
    }
    else
    {
        b[i] = a[j];
        j++;
    }
    i++;
}
if (l > mid)
{
    for (k = j; k <= high; k++)
    {
        b[i] = a[k];
        i++;
    }
}
else
{
    for (k = l; k <= mid; k++)
    {
         b[i] = a[k];
         i++;
    }
}

for (k = low; k <= high; k++)
{
    a[k] = b[k];
}
}

这部分代码通过放入一个辅助数组进行归并排序

  void merge(struct Empoly emp,int high)
  {
 int mid;

if(low < high)
{
    mid = (low + high) / 2;
    merge(a,low,mid);
    merge(a,mid + 1,high);
    mergeSort(a,mid,high);
}


 void rd(struct Employ emp[],int n)
 {
int i;
for(i=0; i<n; i++){
    printf("\nEmployee id: ");
    scanf("%d",&emp[i].id);
    printf("Employee name: ");
    scanf("%s",emp[i].name);
    printf("Employee age: ");
    scanf("%d",&emp[i].age);
    printf("Designation: ");
    scanf("%s",&emp[i].desg);
    fflush(stdin);
    printf("GRade:  ");
    scanf("%c",&emp[i].sec);
    }

    printf("\nEntered details \n");
      for(i=0; i<n; i++){
    printf("\nEmployee Id:   %d\n",emp[i].id);
    printf("Name: %s\n",emp[i].name);
    printf("Age:  %d\n",emp[i].age);
    printf("Designation:   %s\n",emp[i].desg);
    printf("grade : %c",emp[i].sec);
}
printf("\n\n");
}

int main() 
{
struct Employ emp[10];
int n,ch,fg = 0,id,age,in; 
int i;
int mid;
char name[100];
do
{

    printf("Press 1 --- to input data\n");
    printf("Press 2 ---- to do mergesort\n");
    printf("Enter your'e choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
            fg=0;
            while(fg==0)
            {
                printf("Enter the total number of employees: ");
                scanf("%d",&n);
                if(n>=1 && n<=10)
                    fg = 1;
                else
                    printf("Invalid input\n");                        
             }
            rd(emp,n);
           case 2:
       merge(emp,n - 1);
     printf("The sorted array\n");
     for(i=0;i<n;i++)
     printf("%d\t",emp[i].age);
  printf("\n\n\n"); 
  break;    
  }

主要问题是链接不同的结构变量,例如,如果我想要对名称进行排序,那么我必须对代码进行的所有更改都进行了更改,因此它需要作为结构数组老化到合并排序函数中。此外使用字符数组我应该改变什么。 我希望使用合并排序按年龄排序。

解决方法

您发布的代码存在一些严重问题。它甚至没有编译,更不用说为结构数组修改 mergeSort()

问题是:

  1. break; 之后没有 case 1: 语句
  2. 没有 default: 情况
  3. merge() 函数的错误调用
  4. 如果用户没有先在merge()数组中输入数据就调用了emp函数,那么初始化n = 0
  5. 如果您想退出 do-while 循环,则没有退出条件。
#include <stdio.h>
#include <stdlib.h>
struct Employ
{
    int id,age;
    char name[100],desg[100];
    char sec;
};

void mergeSort(struct Employ emp[],int low,int mid,int high)
{
    int i,j,k,l;
    // Here you need a temporary array of type struct like below
    struct Employ b[50];
    l = low;
    i = low;
    j = mid + 1;
    while ((l <= mid) && (j <= high))
    {
        /*
        the if condition below decides on which parameter
        the sorting is done. Age is used below.
        For sorting by name in alphabetical order,use stcmp() function
        if (strcmp(emp[l].name,emp[j].name)<=0)
        */
        if (emp[l].age <= emp[j].age)
        {
            b[i] = emp[l];
            l++;
        }
        else
        {
            b[i] = emp[j];
            j++;
        }
        i++;
    }
    if (l > mid)
    {
        for (k = j; k <= high; k++)
        {
            b[i] = emp[k];
            i++;
        }
    }
    else
    {
        for (k = l; k <= mid; k++)
        {
             b[i] = emp[k];
             i++;
        }
    }

    for (k = low; k <= high; k++)
    {
        emp[k] = b[k];
    }
}

void merge(struct Employ emp[],int high)
{
    int mid;
    if(low < high)
    {
        mid = (low + high) / 2;
        merge(emp,low,mid);
        merge(emp,mid + 1,high);
        mergeSort(emp,mid,high);
    }
}


void rd(struct Employ emp[],int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        printf("\nEmployee id: ");
        scanf("%d",&emp[i].id);
        printf("Employee name: ");
        scanf("%s",emp[i].name);
        printf("Employee age: ");
        scanf("%d",&emp[i].age);
        printf("Designation: ");
        scanf("%s",&emp[i].desg);
        fflush(stdin);
        printf("GRade:  ");
        scanf(" %c",&emp[i].sec);
    }

    printf("\nEntered details \n");
    for(i=0; i<n; i++)
    {
        printf("\nEmployee Id:   %d\n",emp[i].id);
        printf("Name: %s\n",emp[i].name);
        printf("Age:  %d\n",emp[i].age);
        printf("Designation:   %s\n",emp[i].desg);
        printf("grade : %c",emp[i].sec);
    }
    printf("\n\n");
}

int main()
{
    struct Employ emp[10];
    int n=0,ch,fg = 0,id,age,in;
    int i;
    int mid;
    char name[100];
    do
    {
        printf("Press 0 --- to end the program\n");
        printf("Press 1 --- to input data\n");
        printf("Press 2 ---- to do mergesort\n");
        printf("Enter your choice\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 0:
                printf("\n\n-----------Program ended----------\n\n");
                return 0;
            case 1:
                fg=0;
                while(fg==0)
                {
                    printf("Enter the total number of employees: ");
                    scanf("%d",&n);
                    if(n>=1 && n<=10)
                        fg = 1;
                    else
                        printf("Invalid input\n");
                }
                rd(emp,n);
                break;
            case 2:
                merge(emp,n - 1);
                printf("The sorted array\n");
                for(i=0;i<n;i++)
                    printf("%d\t",emp[i].age);
                printf("\n\n\n");
                break;
            default:
                printf("Incorrect choice\n");
        }
    }while(1);
}
Input/Output example:

Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
1
Enter the total number of employees: 3
Employee Id:   1
Name: abc1
Age:  25
Designation:   xyz
grade : A
Employee Id:   2
Name: abc1
Age:  45
Designation:   xyz
grade : A
Employee Id:   3
Name: abc3
Age:  12
Designation:   xyz
grade : A

Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
2
The sorted array
12  25  45  


Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
33
Incorrect choice
Press 0 --- to end the program
Press 1 --- to input data
Press 2 ---- to do mergesort
Enter your choice
0


-----------Program ended----------

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。