| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 354 人关注过本帖
标题:冒泡排序求解,运行时出错,为什么
只看楼主 加入收藏
nan1888
Rank: 2
等 级:论坛游民
帖 子:44
专家分:86
注 册:2011-6-19
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:1 
冒泡排序求解,运行时出错,为什么
#include<iostream>
using namespace std;
#define max 10
struct node{
    int key;
}sqlist[max];
void doubledouble(struct node r[],int n)
{int i=0,j=n-1,b=1,l;
struct node t;
while(b)
{b=0;
for(l=j;l>i;i--)
if(r[l].key<r[l-1].key)
{
    b=1;

        t=r[l];
r[l]=r[l-1];
r[l-1]=t;}
i++;
for(l=i;l<j;l++)
if(r[l].key>r[l-1].key)
{b=1;
t=r[l-1];
r[l-1]=r[l];
r[l]=t;}
j--;
}
}

int main()
{int i,n,temp;
struct node r[10];
cout<<"输入元素个数"<<endl;
cin>>n;
for(i=0;i<n;i++)
{cin>>temp;
r[i].key=temp;
}
doubledouble(r,n);
for(i=0;i<n;i++)
cout<<r[i].key<<endl;
}

[ 本帖最后由 nan1888 于 2011-8-31 18:22 编辑 ]
搜索更多相关主题的帖子: include 元素 
2011-08-31 18:19
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:20 
程序代码:
#include<iostream>
using namespace std;
#define max 10
struct node{
    int key;
}sqlist[max];                    //这里定义的sqlist[max]倒底用啥用,求解释。
void doubledouble(struct node r[],int n)    //这个函数名真有个性
{
    int i=0,j=n-1,b=1,l;                    //这个l很像1
    struct node t;
    while(b)                                //一个冒泡排序不用这么复杂吧
    {
        b=0;
        for(l=j;l>i;i--)
        if(r[l].key<r[l-1].key)
        {
            b=1;
            t=r[l];
            r[l]=r[l-1];
            r[l-1]=t;
        }
        i++;
        for(l=i;l<j;l++)
            if(r[l].key>r[l-1].key)
            {
                b=1;
                t=r[l-1];
                r[l-1]=r[l];
                r[l]=t;
            }
            j--;
    }
}

int main()
{
    int i,n,temp;
    struct node r[10];
    cout<<"输入元素个数"<<endl;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>temp;
        r[i].key=temp;                    //我在想要是n大于10怎么办,其实可以用new的,另外,cin>>r[i].key就可以了
    }
    doubledouble(r,n);
    for(i=0;i<n;i++)
    cout<<r[i].key<<endl;   
}
                                          //最后麻烦楼主把代码摆得好看点行不!
下面是我改后的:
程序代码:
#include<iostream>
using namespace std;

struct node{
    int key;
};
void doubledouble(struct node r[],int n)                //函数名没有改!
{
    struct node temp;
    for(int i = 0; i != n; ++i)
    {
        for(int j = n-1; j != i ; --j)
        {
            if(r[j].key < r[j-1].key)
            {
                temp=r[j];
                r[j]=r[j-1];
                r[j-1]=temp;
            }
        }
    }
}

int main()
{
    int i,n,temp;
    cout<<"输入元素个数"<<endl;
    cin>>n;
    struct node * r=new struct node[n];
    for(i=0;i<n;++i)
        cin>>r[i].key;
    doubledouble(r,n);
    for(i=0;i<n;i++)
    cout<<r[i].key<<endl;
}

2011-08-31 18:59
快速回复:冒泡排序求解,运行时出错,为什么
数据加载中...
 
   



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

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