C 仅当最后一个值最大时,快速排序功能才起作用
我正在尝试在C中使用快速排序。我的程序是一个简单的结构数组,它接受命令行参数(name1 age 1 name2 age 2…等),并按降序输出所述的年龄 仅当最后输入的年龄最大时,它才能正常工作。除此之外,我要么没有输出,要么Seg故障11。有人有什么想法吗C 仅当最后一个值最大时,快速排序功能才起作用,c,arrays,sorting,struct,C,Arrays,Sorting,Struct,我正在尝试在C中使用快速排序。我的程序是一个简单的结构数组,它接受命令行参数(name1 age 1 name2 age 2…等),并按降序输出所述的年龄 仅当最后输入的年龄最大时,它才能正常工作。除此之外,我要么没有输出,要么Seg故障11。有人有什么想法吗 #include <stdio.h> #include <stdlib.h> #include <string.h> #define NameLen 80 void print_struct(); s
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NameLen 80
void print_struct();
struct people
{
char name [NameLen + 1];
int age;
}; //defining a structure//
typedef struct people PERSON;
void quicksort(struct people list[],int,int);
int main(int argc, const char * argv[])
{
int i,j;
j = 0;
int l = ((argc/2)-1);
struct people list[l]; //maximum size of array of structs
if (argc %2 == 0) //if the number of arguments is an even number
{
printf("Invalid Arguments!\n");
printf("Usage : ./hw5 name1 age1 name2 age2 ... "); //print error message and correct program usage
exit(0);
}
printf("You have entered %d persons(s) into the program \n",(argc/2));
for (i=1; i < argc; i+=2)
{
strcpy(list[j].name, argv[i]);
list[j].age = atoi(argv[i+1]);
if(list[j].age == 0)
{
printf("...Invalid age <=0. Try again.\n");
exit(0);
}
j++;
}
printf("Unsorted Names: \n");
print_struct(&list,argc);
printf ("Sorted by Age: \n");
quicksort(list,0 ,j);
for(i=0;i<j;i++){
printf("Name : %s| Age : %d\n", list[i].name, list[i].age);}//possible error here?
//Quicksort Function
#包括
#包括
#包括
#定义名称80
无效打印结构();
结构人
{
字符名[NameLen+1];
智力年龄;
}; //定义结构//
类型定义结构人;
无效快速排序(结构人员列表[],int,int);
int main(int argc,const char*argv[]
{
int i,j;
j=0;
int l=((argc/2)-1);
结构人员列表[l];//结构数组的最大大小
if(argc%2==0)//如果参数的数目是偶数
{
printf(“无效参数!\n”);
printf(“用法:./hw5 name1 age1 name2 age2…”);//打印错误消息并更正程序用法
出口(0);
}
printf(“您已在程序中输入%d人”;(argc/2));
对于(i=1;i printf(“…无效年龄可能问题在于j的值。j是列表的长度?还是列表的长度-1
看起来这就是你想要的:
j=列表的长度
printf ("Sorted by Age: \n");
quicksort(list,0 ,j-1);
for(i=0;i<j;i++){
printf("Name : %s| Age : %d\n", list[i].name, list[i].age);}
printf(“按年龄排序:\n”);
快速排序(列表,0,j-1);
对于(i=0;i而言,快速排序功能很好。问题是您调用它时出错:
quicksort(list,0 ,j);
为first
和last
传递的值表示第一个和最后一个元素的索引。从使用j
循环元素的方式可以明显看出,j
是元素数。这意味着最后一个元素具有索引j-1
因此,您正在为last
传递一个值,该值是数组末尾的一个元素。然后,当您尝试读取/写入此伪元素时,您将调用该值,在您的情况下(幸运的是),该值会导致segfault
传入最后一个元素的实际索引(即比大小小一个),它将成功运行
quicksort(list,0 ,j - 1);
您的循环不正确:
while(list[i].age<=list[pivot].age&&i<last)
i++;
在这里,由于j
以last
开头,因此您可以从读取数组外部开始
while(list[j].age>list[pivot].age)
j--;
一种可能的补救方法是先向后移动j
,然后测试(do
-while
风格)。然后,增加i
,针对减少的j
进行测试
do --j; while(list[j].age>list[pivot].age);
do ++i; while(list[i].age<=list[pivot].age&&i<j);
do--j;while(list[j].age>list[pivot].age);
do++i;while(list[i].age我将代码简化了一点(将字符数组更改为一个字符,使其尽可能简单)。
我的想法是,当你打电话时:
quicksort(list,0 ,j);
你应该称之为:
quicksort(list,0 ,j-1);
因为最后一个参数必须是数组长度减去1,最后一个位置
我没有seg错误,当运行您的代码或我修改的代码时,如果可能,请仔细检查您用作输入的字符串
这是代码的“我的”版本
希望能有帮助
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NameLen 80
void print_struct();
struct people
{
char name;
int age;
}; //defining a structure//
typedef struct people PERSON;
void quicksort(struct people list[],int,int);
int main(int argc, const char * argv[])
{
int i,j;
j = 10;
struct people list[10]; //maximum size of array of structs
for (i=0; i < 10; i++)
{
list[i].name = 'a';
list[i].age = 10-i;
}
printf("Unsorted Names: \n");
for(i=0;i<j;i++){
printf("Name : %c| Age : %d\n", list[i].name, list[i].age);}//possible error here?
printf ("Sorted by Age: \n");
quicksort(list,0 ,j-1);
for(i=0;i<j;i++){
printf("Name : %c| Age : %d\n", list[i].name, list[i].age);}//possible error here?
}
void quicksort(struct people list[],int first,int last)
{
struct people temp;
int i,j,pivot;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j)
{
while(list[i].age<=list[pivot].age&&i<last)
i++;
while(list[j].age>list[pivot].age)
j--;
if(i<j){
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
temp=list[pivot];
list[pivot]=list[j];
list[j]=temp;
quicksort(list,first,j-1);
quicksort(list,j+1,last);
}
}
#包括
#包括
#包括
#定义名称80
无效打印结构();
结构人
{
字符名;
智力年龄;
};//定义结构//
类型定义结构人;
无效快速排序(结构人员列表[],int,int);
int main(int argc,const char*argv[]
{
int i,j;
j=10;
结构人员列表[10];//结构数组的最大大小
对于(i=0;i<10;i++)
{
列表[i]。名称='a';
列表[i]。年龄=10-i;
}
printf(“未排序的名称:\n”);
对于(i=0;i,在应用所有注释后,这是生成的代码,它可以干净地编译,但由于缺少两个函数而不会链接:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN (80)
struct people
{
char name [NAME_LEN + 1];
int age;
}; //defining a structure//
typedef struct people PERSON;
// print_struct( ptrToList, numElements )
void print_struct( PERSON *, int );
// quicksort( ptrToList, firstOffset, numElements )
void quicksort(struct people list[],int,int);
int main(int argc, const char * argv[])
{
//int i,j;
//j = 0;
//int l = ((argc/2)-1);
int l = argc/2;
struct people list[l]; //maximum size of array of structs
if (argc %2 == 0) //if the number of arguments is an even number
{
//printf("Invalid Arguments!\n");
fprintf( stderr, "Invalid Arguments!\n" );
//printf("Usage : ./hw5 name1 age1 name2 age2 ... "); //print error message and correct program usage
fprintf( stderr, "Usage : %s name1 age1 name2 age2 ... ", argv[0]);
// exit(0);
exit( EXIT_FAILURE );
}
//printf("You have entered %d persons(s) into the program \n",(argc/2));
printf("You have entered %d persons(s) into the program \n", l);
//for (int i=1; i < argc; i+=2)
for (int i=1, j=0; j < l; i+=2, j++)
{
//strcpy(list[j].name, argv[i]);
memset( list[i].name, '\0', NAME_LEN+1);
strncpy( list[j].name, argv[i], NAME_LEN );
list[j].age = atoi(argv[i+1]);
if(list[j].age == 0)
{
fprintf( stderr, "...Invalid age <=0. Try again.\n");
//exit(0);
exit( EXIT_FAILURE );
}
//j++;
}
printf("Unsorted Names: \n");
//print_struct(&list,argc);
print_struct( list, l );
//printf ("Sorted by Age: \n");
//quicksort(list,0 ,j);
quicksort( list, 0, l );
printf ("Sorted by Age: \n");
// //for(i=0;i<j;i++)
//for( int i=0; i<l; i++ )
//{
// printf("Name : %s| Age : %d\n", list[i].name, list[i].age);
//}//possible error here?
//}
print_struct( list, l);
} // end function: main
//Quicksort Function
#包括
#包括
#包括
#定义名称(80)
结构人
{
字符名[name_LEN+1];
智力年龄;
};//定义结构//
类型定义结构人;
//打印结构(ptrToList、NUMELENTS)
无效打印结构(个人*,整数);
//快速排序(ptrToList、firstOffset、numElements)
无效快速排序(结构人员列表[],int,int);
int main(int argc,const char*argv[]
{
//int i,j;
//j=0;
//int l=((argc/2)-1);
int l=argc/2;
结构人员列表[l];//结构数组的最大大小
if(argc%2==0)//如果参数的数目是偶数
{
//printf(“无效参数!\n”);
fprintf(stderr,“无效参数!\n”);
//printf(“用法:./hw5 name1 age1 name2 age2…”);//打印错误消息并更正程序用法
fprintf(标准,“用法:%s name1 age1 name2 age2…”,argv[0]);
//出口(0);
退出(退出失败);
}
//printf(“您已在程序中输入%d人”;(argc/2));
printf(“您在程序中输入了%d人,\n”,l);
//对于(int i=1;i fprintf(stderr,“…保持一致的代码样式肯定会提高可读性。谢谢@Kupiakos!这是我第二次发帖,所以我会继续写下去!为了便于阅读和理解1)一致缩进代码2)使用一致的垂直间距3)遵循公理:每行只有一条语句,并且(最多)每条语句一个变量声明。发布的代码未编译。它缺少函数main()
的结尾。此行用于计算列表[]
数组的大小:int l=((argc/2)-1);
不正确。这是一个整数除法。如果argc
为7,则l
的结果值应为3,但是,计算结果为2。建议从表达式中删除-1
,否则代码为