阅读背景:

第十三周项目4 字符数组排序

来源:互联网 
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2014年11月20日
*版本号:v1.0
*
*问题描述:数组排序
*输入描述:无
*程序输出:数组排序
*/
#include <iostream>
using namespace std;
void bubble_sort(char a[],int n);
void butput_array(char a[],int n);
int main()
{
    char a[18]= {'s','o','r','t','b','u','b','b','l','e','s','e','l','e','c','t','o','k'};
    char b[10]= {'a','b','a','l','e','v','t','w','y','z'};
    bubble_sort(a,18);
    cout<<endl;
    butput_array(a,18);
    cout<<endl;
    bubble_sort(b,10);
    cout<<endl;
    butput_array(b,10);
    cout<<endl;
    return 0;
}
void bubble_sort(char a[],int n)
{
    int i,j;
    char t;
    for(j=0; j<n; ++j)
    {
        for(i=0; i<n-j-1; ++i)            if(a[i]<=a[i+1])
            {
                t=a[i];
                a[i]=a[i+1];
                a[i+1]=t;
            }
    }
    cout<<"用冒泡法降序排序为:"<<endl;
    for(i=0; i<n; ++i)
        cout<<a[i]<<" ";
}
void butput_array(char a[],int n)
{
    int x,y;
    char z;
    for(x=0; x<n-1; ++x)        for(y=0; y<n-1; ++y)
            if(a[y]>a[y+1])
            {
                z=a[y];
                a[y]=a[y+1];
                a[y+1]=z;
            }
    cout<<"排序后的数组为:"<<endl;
    for(y=0; y<n; ++y)
        cout<<a[y]<<" ";
}


/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights re



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: