C-为啥Struct数组的冒泡排序会跳过Array中的第一个元素?
Posted
技术标签:
【中文标题】C-为啥Struct数组的冒泡排序会跳过Array中的第一个元素?【英文标题】:C-Why does Bubble Sort of Struct array skip the first element in the Array?C-为什么Struct数组的冒泡排序会跳过Array中的第一个元素? 【发布时间】:2016-03-12 00:42:30 【问题描述】:有谁知道为什么这种排序不对结构数组中的第一个元素进行排序?下面是结构、排序和一些示例输入输出的代码。我已经排除了填充数组的代码,因为我知道在排序之前打印数组,它是正确填充的。
struct Record
char *firstName;
char *lastName;
char *GPA;
unsigned int ID;
;
void newList(struct Record * Records, int amount, char* name)
int i;
int j;
struct Record tempR;
FILE *fp;
unsigned int temp;
//Order Array
for(i=0;i<amount;i++)
for(j=0;j<amount-1;j++)
if(strcmp(Records[j].firstName,Records[j+1].firstName)<0)
tempR=Records[j];
Records[j]=Records[j+1];
Records[j+1]=tempR;
//Make New Fle with Ordered Array
fp=fopen(name, "w+");
for(i=0;i<amount;i++)
fprintf(fp,"%s, %s, %s, %d, Records[i].firstName,Records[i].lastName,
Records[i].GPA,Records[i].ID);
此代码的示例输入和输出如下输入:
Yblwtjbvtz,Eoztbzoqnz,2.6,1123268861
Blmhwgzjdd,Ojwfnlislc,3.1,1712113924
Gkmkbnotic,Mhzcakkugv,3.3,1966045151
Zsrwqdwkfo,Nciqixcamr,2.1,212426241
Vrekafrafk,Ixylzenhlc,2.2,297694159
Kzkaxpoeqg,Syawkuqbew,3.4,104209687
Ekdcfsifrw,Apvrwfshqm,1.4,799470314
Iybmcotvpf,Eqvcorjntu,0.6,1748600414
Jsfwiydnyt,Rhyaabwfdr,2.2,104800253
Mfqrukoytp,Urjsjcloau,3.8,1240702350"
输出:
Yblwtjbvtz, Eoztbzoqnz, 2.6, 1123268861
Zsrwqdwkfo, Nciqixcamr, 2.1, 212426241
Vrekafrafk, Ixylzenhlc, 2.2, 297694159
Mfqrukoytp, Urjsjcloau, 3.8, 1240702350
Kzkaxpoeqg, Syawkuqbew, 3.4, 104209687
Jsfwiydnyt, Rhyaabwfdr, 2.2, 104800253
Iybmcotvpf, Eqvcorjntu, 0.6, 1748600414
Gkmkbnotic, Mhzcakkugv, 3.3, 1966045151
Ekdcfsifrw, Apvrwfshqm, 1.4, 799470314
Blmhwgzjdd, Ojwfnlislc, 3.1, 1712113924
【问题讨论】:
您需要理顺代码格式。要正确格式化代码:1)从源文件中复制代码 2)将代码粘贴到问题中 3)选择问题中的代码 4)单击代码按钮
或按 ctrl-K
Couldn't reproduce 进行一些不会影响逻辑的修改。请发帖Minimal, Complete, and Verifiable example。
显然你还差一关,for(i=0;i<amount;i++)
,但我不明白为什么。另外你在浪费周期,应该是for(j=0;j<amount-1-i;j++)
@xvan “Z”条目从第 4 行开始,因此它应该替换了第三遍中的“Y”条目。所以我同意 MikeCAT 所显示的代码与输出不匹配。
强烈建议:将冒泡排序算法放在一个单独的函数中,可能命名为bubbleSort()
最好在一个函数中只执行一个明确定义的动作
【参考方案1】:
这是一个用 C 编写的冒泡排序算法示例。
for (size_t c = 0 ; c < ( n - 1 ); c++)
for (size_t d = 0 ; d < n - c - 1; d++)
if (array[d] > array[d+1]) /* For decreasing order use < */
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
所以发布的代码应该实现相同的算法,只是使用不同的变量/数组名称
【讨论】:
Ty 代码示例!我试图实现它,但是当我这样做时,输出给出了......我会在下面发布它【参考方案2】:以下代码实现所有 cmets,执行适当的错误检查,并执行所需的操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Record
char *firstName;
char *lastName;
char *GPA;
unsigned int ID;
;
static struct Record Records[] =
"Yblwtjbvtz","Eoztbzoqnz","2.6",1123268861,
"Blmhwgzjdd","Ojwfnlislc","3.1",1712113924,
"Gkmkbnotic","Mhzcakkugv","3.3",1966045151,
"Zsrwqdwkfo","Nciqixcamr","2.1",212426241,
"Vrekafrafk","Ixylzenhlc","2.2",297694159,
"Kzkaxpoeqg","Syawkuqbew","3.4",104209687,
"Ekdcfsifrw","Apvrwfshqm","1.4",799470314,
"Iybmcotvpf","Eqvcorjntu","0.6",1748600414,
"Jsfwiydnyt","Rhyaabwfdr","2.2",104800253,
"Mfqrukoytp","Urjsjcloau","3.8",1240702350
;
//prototypes
void newList( char* filename );
void printList( char* filename );
int main( void )
newList( "sorted.txt" );
printList( "sorted.txt" );
return 0;
// end function: main
void newList( char* filename )
struct Record swap;
size_t numRecords = sizeof( Records)/sizeof( struct Record);
//Order Array, ascending on firstName field
for (size_t c = 0 ; c < ( numRecords - 1 ); c++)
for (size_t d = 0 ; d < numRecords - c - 1; d++)
if ( 0 < strcmp(Records[d].firstName, Records[d+1].firstName) )
memcpy( &swap, &Records[d], sizeof( struct Record ) );
memcpy( &Records[d], &Records[d+1], sizeof( struct Record ) );
memcpy( &Records[d+1], &swap, sizeof( struct Record ) );
//Make New File with Ordered Array
FILE *fp = NULL;
if( NULL == (fp=fopen(filename, "w") ) )
// then, fopen failed
perror( "fopen sorted.txt for write failed" );
exit( EXIT_FAILURE );
// implied else, fopen successful
for(size_t i=0;i<numRecords;i++)
fwrite( &Records[i], sizeof( struct Record ), 1, fp );
fclose( fp );
// end function: newList
void printList( char * filename )
FILE *fp = NULL;
if( NULL == (fp=fopen(filename, "r") ) )
// then, fopen failed
perror( "fopen sorted.txt for read failed" );
exit( EXIT_FAILURE );
struct Record oneRecord;
while(1 == fread( &oneRecord, sizeof( struct Record ), 1, fp) )
printf( "%s %s %s %u\n",
oneRecord.firstName,
oneRecord.lastName,
oneRecord.GPA,
oneRecord.ID );
fclose( fp );
// end function: printList
这是程序的输出:
Blmhwgzjdd Ojwfnlislc 3.1 1712113924
Ekdcfsifrw Apvrwfshqm 1.4 799470314
Gkmkbnotic Mhzcakkugv 3.3 1966045151
Iybmcotvpf Eqvcorjntu 0.6 1748600414
Jsfwiydnyt Rhyaabwfdr 2.2 104800253
Kzkaxpoeqg Syawkuqbew 3.4 104209687
Mfqrukoytp Urjsjcloau 3.8 1240702350
Vrekafrafk Ixylzenhlc 2.2 297694159
Yblwtjbvtz Eoztbzoqnz 2.6 1123268861
Zsrwqdwkfo Nciqixcamr 2.1 212426241
【讨论】:
我实现了这个......它仍然无法正常工作。谢谢你的代码!这一定和Putty有关。因为逻辑在那里......没有理由这不应该工作以上是关于C-为啥Struct数组的冒泡排序会跳过Array中的第一个元素?的主要内容,如果未能解决你的问题,请参考以下文章