字符数组的排列

/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 作    者:赵加响
* 完成日期:2013  年12  月 2 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:数组的排序
* 程序输出:
*问题分析:
*/
#include <iostream>
using namespace std;
//两个函数bubble_sort和output_array的声明
void bubble_sort(char[], char);
void output_array(char[],char);
int main( )
{
    char a[20] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'};
    char b[15] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',};//请自己补足
    bubble_sort(a,20);   //用冒泡法按降序排序a中元素
    output_array(a,20);
    cout<<endl;   //输出排序后的数组
    bubble_sort(b,15);   //用冒泡法按降序排序b中元素
    output_array(b,15);
    cout<<endl;   //输出排序后的数组
    return 0;
}
//请在下面定义bubble_sort和output_array
void bubble_sort(char a[],char n)
{
   char i,t;
    for(i=1; i<n; i++)//n个数 比较n-1次
        for(char j=0; j<n-i; j++)
            if(a[j+1]>a[j])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
}
void output_array(char a[],char n)
{
    for(char i=0; i<n; i++)
        cout<<a[i]<<" ";
}


 

原文地址:https://www.cnblogs.com/fuhaots2009/p/3455504.html