您的位置:首页 > 其它

冒泡法排序及排序结果校验

2013-07-08 16:23 169 查看
#include <iostream>

bool CheckSort(int * pSource, int N);
bool Sort(int * pSource, int N);

int main()
{
    using std::cout;
    using std::endl;
    const int SIZE =10;
    int a[10] ={-65,10,33,-2,81,-89,0,14,142,3};
    bool isSort = false,isSortStatus;

    isSortStatus =CheckSort(a,SIZE);
    if (isSortStatus)
       cout <<"Sorted...\n";
    else
       cout <<"Unsorted..\n";
   /////////////////////////////////////////////
    cout<<">>>Sorting...\n";
    isSort =Sort(a,SIZE);
    if (isSort)
       for (int i = 0; i
           cout<< i+1<< "--->"<< a[i]<< endl;
    else
       cout << "Sortfail.\n";
   ////////////////////////////////////////////
    isSortStatus =CheckSort(a,SIZE);
    if (isSortStatus)
       cout <<"Sorted...\n";
    else
       cout <<"Unsorted..\n";
    return 0;
}

bool CheckSort(int * pSource, int N)
{
    bool Check =false;
    for (int i=0; i
    {
       if ( *(pSource+i) <=*(pSource+i+1) )
           Check =true;
       else
           Check =false;
       if (!Check)
          break;
    }
    return Check;
}

bool Sort(int * pSource, int N)
{
    int tmp;
    for (int i=0; i
    {
       for (int j=0; j
       {
           if (*(pSource+j) > *(pSource+j+1) )
           {
              tmp = *(pSource+j);
              *(pSource+j) =*(pSource+j+1);
              *(pSource+j+1) = tmp;
           }
       }
    }
    return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: