请教高手这个程序的快速排序法部分哪里错了?只是快速排序法部分。 编译器是Microsoft Visul C++

题目要求1使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2用快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是... 题目要求
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);
2 用快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。

#include<stdio.h>
#include<string.h>
#include<iostream.h>
struct StudentInfo
{ char ID[11];
char * name;
float score;
}StuInfo[12]=
{
{"0800301105", "JACK", 95},
{"0800201505", "LUN", 85},
{"0400820115", "MARY", 75.5},
{"0400850122", "KATE", 78.9},
{"0500201011", "LILI", 88},
{"0800401105", "JACK", 96},
{"0600830105", "JAN", 98.4},
{"0952520012", "SAM", 75},
{"9721000045", "OSCAR", 64},
{"0700301105", "JACK", 97},
{"0458003312", "ZOE", 68.9},
{"0400830211", "BOBI", 87.6}
};
void InsertionSort( StudentInfo A[ ], int N )//直接插入排序
{
int j, P;
StudentInfo Tmp;
for( P = 1; P < N; P++ )
{
Tmp = A[ P ];
for( j = P; j > 0 &&strcmp( A[ j - 1 ].ID ,Tmp.ID)>0; j-- )
A[ j ] = A[ j - 1 ];
A[ j ] = Tmp;
}
}
void Quicksort( StudentInfo A[ ], int N )//快速排序
{
Qsort( A, 0, N - 1 );
}
#define Cutoff ( 3 )

void Qsort( StudentInfo A[ ], int Left, int Right )
{
int i, j;
StudentInfo Pivot;

if( strcmp(strcpy(Left,Cutoff),Right)<0 )
{
Pivot = Median3( A, Left, Right );
i = Left; j = Right - 1;
for( ; ; )
{
while( strcmp(A[ ++i ],Pivot)<0){ }
while( strcmp(A[ --j ],Pivot)>0 ){ }
if( i < j )
Swap( &A[ i ], &A[ j ] );
else
break;
}
Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */

Qsort( A, Left, i - 1 );
Qsort( A, i + 1, Right );
}
else /* Do an insertion sort on the subarray */
InsertionSort( A + Left, Right - Left + 1 );
}
void Swap(char a,char b)
{
char c;
c=a;
b=a;
a=b;
}
Median3( StudentInfo A[ ], int Left, int Right )
{
int Center = ( Left + Right ) / 2;

if( strcmp(A[ Left ],A[ Center ])>0 )
Swap( &A[ Left ], &A[ Center ] );
if( strcmp(A[ Left ],A[ Right ] )>0)
Swap( &A[ Left ], &A[ Right ] );
if( strcmp(A[ Center ],A[ Right ])>0 )
Swap( &A[ Center ], &A[ Right ] );

/* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */

Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */
return A[ Right - 1 ]; /* Return pivot */
}

void main()
{
for(int i=0;i<12;i++)
InsertionSort(StuInfo,12);
for(i=0;i<12;i++)
printf("%s \n",StuInfo[i].ID);
printf("\n");
Qsort( StuInfo, Left, Right )

}
不好意思 最后一行的Qsort( StuInfo, Left, Right )因该是Quicksort( StuInfo, 12 )
展开
 我来答
匿名用户
2011-04-16
展开全部

感觉错误很多。。1 不能用结构体直接赋值 例如:StudentInfo A[ 11],StudentInfo Tmp;

A[0]=Tmp;2 void Swap(char a,char b)声明定义 类型不对吧 是不是想用 StudentInfo 型  而且 应该是地址或引用 void Swap(StudentInfo *a,StudentInfo *b)或

void Swap(StudentInfo &a,StudentInfo &b)   3 感觉应该把声明定义顺序改下 让后用到函数知道前面已经声明了 要使用的函数 4 strcmp(strcpy(Left,Cutoff),Right)<0  这样使用函数应该是不对的 参数不匹配  5这种情况排序 似乎用链表会好点  我重新做了个 我也不知道 我做的是不是你想要的  你参考下 结果是运行时 先按ID 排序 然后再点一下键盘 是按Name排序 

#include<stdio.h>

#include<string.h>

//#include<iostream.h>

struct StudentInfo

{ char ID[11];

 char * name;

 float score;

}StuInfo[12]=

{

{"0800301105", "JACK", 95},

{"0800201505", "LUN", 85},

{"0400820115", "MARY", 75.5},

{"0400850122", "KATE", 78.9},

{"0500201011", "LILI", 88},

{"0800401105", "JACK", 96},

{"0600830105", "JAN", 98.4},

{"0952520012", "SAM", 75},

{"9721000045", "OSCAR", 64},

{"0700301105", "JACK", 97},

{"0458003312", "ZOE", 68.9},

{"0400830211", "BOBI", 87.6} 

}; 

void Swap(StudentInfo *a,StudentInfo *b)

 {

  StudentInfo c;

 

   strcpy(c.ID,a->ID);

    strcpy(a->ID,b->ID);

    strcpy(b->ID,c.ID);

 

     c.name=a->name;

    a->name=b->name;

    b->name=c.name;

      

      c.score = a->score;

      a->score=b->score;

      b->score=c.score;

  }

 void InsertionSort( StudentInfo A[], int N )//直接插入排序

 {

            int j, p=0;

           // StudentInfo Tmp={"\0","\0",0};

for( j=N-1;j>=0;j--)

{  

   for(p=0;p<j;p++){

    

    if((strcmp(A[p].ID,A[p+1].ID)>0))

  Swap(&A[p],&A[p+1]);

 

 }

  

}   

 }

    

 

void Qsort( StudentInfo A[ ], int N ){

     int j, p=0;

  // StudentInfo Tmp={"\0","\0",0};

for( j=N-1;j>=0;j--)

{  for(p=0;p<j;p++){

   if((strcmp(A[p].name,A[p+1].name)>0)) 

Swap(&A[p],&A[p+1]);

   }    

 }

}  

 void main()

 {

  

InsertionSort(StuInfo,12);

 printf("  ID:\n");

  for(int i=0;i<12;i++)

{

printf("%2d|%s\t",i+1,StuInfo[i].ID);

printf("%s\t",StuInfo[i].name);

printf("%f\n",StuInfo[i].score);

}

 getchar();

Qsort( StuInfo,12);

printf("  Name:\n");

  for( i=0;i<12;i++)

{printf("%2d|%s\t",i+1,StuInfo[i].ID);

printf("%s\t ",StuInfo[i].name);

printf("%f  ",StuInfo[i].score);

if(i%2==1)printf("\n");

}

 

 }

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式