标题:bubble sort 问题
取消只看楼主
fairsky1314
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-8-6
结帖率:66.67%
已结贴  问题点数:20 回复次数:3 
bubble sort 问题
#include <stdio.h>
#define MAXDATA 1000
struct elem {
    char name[16];
    int x,y;
}elem[MAXDATA+1];
   
int max;
int main(){
    /*input*/
    printf("*********************.\n");
    printf("*sort start ***\n");
    printf("*********************.\n");
    set_data();
    sort_data();
    dump_data();
    printf("*********************.\n");
    printf("*sort finish ***\n");
    printf("*********************.\n");
    return 0;
}
int set_data(){
int i;
    strcpy(elem[0].name,"A");elem[0].x = 1; elem[0].y = 2;
    strcpy(elem[1].name,"B");elem[1].x = 3; elem[1].y = 3;
    strcpy(elem[2].name,"C");elem[2].x = 2; elem[2].y = 4;
    strcpy(elem[3].name,"D");elem[3].x = 4; elem[3].y = 6;
    strcpy(elem[4].name,"E");elem[4].x = 5; elem[4].y = 8;
    strcpy(elem[5].name,"F");elem[5].x = 2; elem[5].y = 1;
    strcpy(elem[6].name,"G");elem[6].x = 4; elem[6].y = 4;
    strcpy(elem[7].name,"H");elem[7].x = 8; elem[7].y = 2;
    strcpy(elem[8].name,"I");elem[8].x = 3; elem[8].y = 1;
    strcpy(elem[9].name,"J");elem[9].x = 2; elem[9].y = 4;
    strcpy(elem[10].name,"K");elem[10].x = 1; elem[10].y = 1;
    strcpy(elem[11].name,"L");elem[11].x = 8; elem[11].y = 2;
    strcpy(elem[12].name,"M");elem[12].x = 3; elem[12].y = 1;
    strcpy(elem[13].name,"N");elem[13].x = 2; elem[13].y = 4;
    strcpy(elem[14].name,"O");elem[14].x = 1; elem[14].y = 9;
    max = 15;
    return 0;
}
int sort_data(){
int cf,i;
}

int dump_data(){
int i;
    printf("*********** dump data: %d ****************.\n", max);
    for(i=0; i<max;i++)
        printf("%s %d %d\n", elem[i].name, elem[i].x, elem[i].y);
    printf("********* dump data end *****************.\n", max);
    return 0;
}

目的 完成
int sort_data(){

}
利用bubble sort方法 把elem[i].x坐标由小到大的顺序把数组重新排列
再把新数组输出
搜索更多相关主题的帖子: include return start 
2011-11-19 14:15
fairsky1314
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-8-6
得分:0 
呵呵  是的
单个的数组学过  例如int x[ ] = {9, 4, 6, 2, 1, 8, 0, 3, 7, 5};
这个排列我会  
但是这种  x , y的
有点不会变通了。。。  请教
2011-11-19 14:43
fairsky1314
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-8-6
得分:0 
没有人给解决一下么?
2011-11-19 15:54
fairsky1314
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-8-6
得分:0 
自己解决了。。
int sort_data(){
int i, j;
 char name_temp[16];
 int x_temp, y_temp;
 
 for(i=0;i< max-1;i++){
 
   
    for(j = 0; j < max-i-1; j++)
    {
        if(elem[j].x > elem[j+1].x )
        {
           strcpy(name_temp, elem[j].name);
           strcpy(elem[j].name, elem[j+1].name);
           strcpy(elem[j+1].name, name_temp);
           
           x_temp = elem[j].x;
           elem[j].x = elem[j+1].x;
           elem[j+1].x = x_temp;
           
           y_temp = elem[j].y;
           elem[j].y = elem[j+1].y;
           elem[j+1].y = y_temp;
           
         
         }
        }

        
    }
return 0;

}
2011-11-21 10:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355680-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.312261 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved