问题描述
#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()
。
问题是:
-
break;
之后没有case 1:
语句 - 没有
default:
情况 - 对
merge()
函数的错误调用 - 如果用户没有先在
merge()
数组中输入数据就调用了emp
函数,那么初始化n = 0
- 如果您想退出 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----------