| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 460 人关注过本帖
标题:高手帮忙看下这个冒泡排序的问题!!!!
只看楼主 加入收藏
BlizzardKen
Rank: 1
来 自:广西桂林
等 级:新手上路
帖 子:36
专家分:0
注 册:2011-4-26
结帖率:33.33%
收藏
 问题点数:0 回复次数:4 
高手帮忙看下这个冒泡排序的问题!!!!

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<time.h>
#define ARR_SIZE 8
using namespace std;
void maopaopaixu(int [],int);
void showarray(int [],int);
int main(int argc, char* argv[])
{
            int lArray[ARR_SIZE];
            srand((unsigned)time(NULL));
            cout<<"随机产生的数组,排序前:"<<endl;
                for(int n=0;n<ARR_SIZE;n++)
                    {
                            lArray[n]=rand();
                            cout<<setw(8)<<lArray[n];
                    }
                cout<<endl;
                cout<<"使用冒泡排序后的数组为:"<<endl;
                maopaopaixu(lArray,ARR_SIZE);
                showarray(lArray,ARR_SIZE);
                cout<<endl;
                return 0;
}
void maopaopaixu(int barray[],int b)
{
    bool exchange=false;
        for(int i=0;i<b-1&&!exchange;i++)————————为什么要设一个   "!exchange"
           {
           exchange=true;
           for(int j=i+1;j<i;j++)              //for(int j=b-1;j>i;j--)
           {
                if(barray[j]<barray[i])       //if(barray[j]<barray[j-1])
                {                                    //{
                        int t=barray[j];         //    int t=barray[j-1];
                        barray[j]=barray[i];   //     barray[j-1]=barray[j];
                        barray[i]=t;               //        barray[j]=t;
                        exchange=false;        // exchange=false;
           }                                           //}
           }
        }
}
void showarray(int carray[],int c)
{

  for(int n=0;n<c;n++)
      cout<<setw(8)<<carray[n];
}

  这句代码: for(int i=0;i<b-1&&!exchange;i++)————————中为什么要设一个   "!exchange“
新人不是很理解,高手帮下忙

[ 本帖最后由 BlizzardKen 于 2011-5-6 12:46 编辑 ]
2011-05-05 20:25
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:0 
代码能看懂吗?加注释的那段是处理排序的,其它是给数组赋值和打印数组的

成功贵在坚持
2011-05-05 20:45
BlizzardKen
Rank: 1
来 自:广西桂林
等 级:新手上路
帖 子:36
专家分:0
注 册:2011-4-26
收藏
得分:0 
回复 2楼 mayuebo
我知道是处理排序的啊, 可是为什么我这样子写:
 for(int j=i+1;j<i;j++)                  
     {
                if(barray[j]<barray[i])   

                      int t=barray[j];      
                     barray[j]=barray[i                     
                     barray[i]=t;                             
                   exchange=false;   
}
不能实现排序这个功能呢?
2011-05-05 20:57
BlizzardKen
Rank: 1
来 自:广西桂林
等 级:新手上路
帖 子:36
专家分:0
注 册:2011-4-26
收藏
得分:0 
回复 2楼 mayuebo
我知道在哪出问题了,原来是我写错了一个代码,导致不能实现排序这个功能
2011-05-05 21:00
renkang任康
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-5-5
收藏
得分:0 
回复 2楼 mayuebo
你这样做  只能把最大的或者最小的  排出来
2011-05-08 19:08
快速回复:高手帮忙看下这个冒泡排序的问题!!!!
数据加载中...
 
   



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

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