据我了解,对于初学者,您需要按数据成员GPA 对 STUDENT 类型的对象进行排序。这意味着在排序过程中,如果需要,您需要交换整个对象,而不是交换数据成员 FPA 的值。
所以这些陈述
temp = StuAry[current].GPA;
//...
StuAry[walk+1].GPA = StuAry[walk].GPA;
//...
StuAry[walk+1].GPA=temp;
没有意义。
在这个for循环中
for(walk = current-1; walk>=0 && !located;)
{
if( temp > StuAry[walk].GPA)
{
StuAry[walk+1].GPA = StuAry[walk].GPA;
walk --;
}
else
located=true;
StuAry[walk+1].GPA=temp;
}
声明
StuAry[walk+1].GPA=temp;
在循环的每次迭代中执行,因为它不是 else 语句的子语句
else
located=true;
StuAry[walk+1].GPA=temp;
该函数可以如下面的演示程序所示。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct
{
int ID;
float GPA;
}STUDENT;
void insertion_sort( STUDENT a[], size_t n )
{
for ( size_t i = 1; i < n; i++ )
{
STUDENT current = a[i];
size_t j = i;
while ( j != 0 && a[j-1].GPA < current.GPA )
{
a[j] = a[j-1];
--j;
}
if ( j != i ) a[j] = current;
}
}
int main(void)
{
enum { N = 10 };
STUDENT a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
a[i].ID = ( int )i;
a[i].GPA = ( rand() % N ) / ( float )N;
}
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
insertion_sort( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
return 0;
}
程序输出可能看起来像
(0, 0.4) (1, 0.4) (2, 0.1) (3, 0.7) (4, 0.3) (5, 0.7) (6, 0.9) (7, 0.1) (8, 0.2) (9, 0.0)
(6, 0.9) (3, 0.7) (5, 0.7) (0, 0.4) (1, 0.4) (4, 0.3) (8, 0.2) (2, 0.1) (7, 0.1) (9, 0.0)
如果您希望如果两个对象具有相同的 GPA 将按照 ID 的升序排序,那么 while 语句中的条件可以如下所示。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct
{
int ID;
float GPA;
}STUDENT;
void insertion_sort( STUDENT a[], size_t n )
{
for ( size_t i = 1; i < n; i++ )
{
STUDENT current = a[i];
size_t j = i;
while ( j != 0 &&
( a[j-1].GPA < current.GPA ||
( !( current.GPA < a[j-1].GPA ) && current.ID < a[j-1].ID ) ) )
{
a[j] = a[j-1];
--j;
}
if ( j != i ) a[j] = current;
}
}
int main(void)
{
enum { N = 10 };
STUDENT a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
a[i].ID = ( int )i;
a[i].GPA = ( rand() % N ) / ( float )N;
}
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
insertion_sort( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
return 0;
}
程序输出可能看起来像
(0, 0.0) (1, 0.7) (2, 0.8) (3, 0.8) (4, 0.4) (5, 0.6) (6, 0.0) (7, 0.3) (8, 0.7) (9, 0.2)
(2, 0.8) (3, 0.8) (1, 0.7) (8, 0.7) (5, 0.6) (4, 0.4) (7, 0.3) (9, 0.2) (0, 0.0) (6, 0.0)
例如前两条 GPA 相同的记录
(2, 0.8) 和 (3, 0.8)
按ID升序排列。
您可以编写一个通用排序函数,该函数接受提供所需条件的函数。例如
void insertion_sort( STUDENT a[], size_t n, int cmp( const void *, const void * ) )
{
for ( size_t i = 1; i < n; i++ )
{
STUDENT current = a[i];
size_t j = i;
while ( j != 0 && cmp( &a[j-1], ¤t ) > 0 )
{
a[j] = a[j-1];
--j;
}
if ( j != i ) a[j] = current;
}
}
所以你只需要提供一个比较函数来比较数组的两个元素。
这是一个演示程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct
{
int ID;
float GPA;
}STUDENT;
int cmp_ascending( const void *a, const void *b )
{
const STUDENT *left = a;
const STUDENT *right = b;
return ( right->GPA < left->GPA ) - ( left->GPA < right->GPA );
}
int cmp_descending( const void *a, const void *b )
{
const STUDENT *left = a;
const STUDENT *right = b;
return ( left->GPA < right->GPA ) - ( right->GPA < left->GPA );
}
void insertion_sort( STUDENT a[], size_t n, int cmp( const void *, const void * ) )
{
for ( size_t i = 1; i < n; i++ )
{
STUDENT current = a[i];
size_t j = i;
while ( j != 0 && cmp( &a[j-1], ¤t ) > 0 )
{
a[j] = a[j-1];
--j;
}
if ( j != i ) a[j] = current;
}
}
int main(void)
{
enum { N = 10 };
STUDENT a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
a[i].ID = ( int )i;
a[i].GPA = ( rand() % N ) / ( float )N;
}
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
insertion_sort( a, N, cmp_ascending );
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
insertion_sort( a, N, cmp_descending );
for ( size_t i = 0; i < N; i++ )
{
printf( "(%d, %.1f) ", a[i].ID, a[i].GPA );
}
putchar( '\n' );
return 0;
}
它的输出可能看起来像
(0, 0.5) (1, 0.9) (2, 0.6) (3, 0.3) (4, 0.2) (5, 0.6) (6, 0.1) (7, 0.3) (8, 0.0) (9, 0.3)
(8, 0.0) (6, 0.1) (4, 0.2) (3, 0.3) (7, 0.3) (9, 0.3) (0, 0.5) (2, 0.6) (5, 0.6) (1, 0.9)
(1, 0.9) (2, 0.6) (5, 0.6) (0, 0.5) (3, 0.3) (7, 0.3) (9, 0.3) (4, 0.2) (6, 0.1) (8, 0.0)
首先对数组进行升序排序,然后再降序排序。