注册 登录
编程论坛 C++教室

高手帮忙看下这个冒泡排序的问题!!!!

BlizzardKen 发布于 2011-05-05 20:25, 460 次点击

#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 编辑 ]
4 回复
#2
mayuebo2011-05-05 20:45
代码能看懂吗?加注释的那段是处理排序的,其它是给数组赋值和打印数组的
#3
BlizzardKen2011-05-05 20:57
回复 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;   
}
不能实现排序这个功能呢?
#4
BlizzardKen2011-05-05 21:00
回复 2楼 mayuebo
我知道在哪出问题了,原来是我写错了一个代码,导致不能实现排序这个功能
#5
renkang任康2011-05-08 19:08
回复 2楼 mayuebo
你这样做  只能把最大的或者最小的  排出来
1