/*
*Copyright (c) 2014, 烟台大学计算机学院
* All right reserved.
* 文件名称:test.cpp
* 作者:陈丹
* 完成时间:2014年11月23号
* 版本号:v1.0
*
*问题描述:编写函数,完成冒泡排序
*输入描述:无输入
*程序输出:输出排序后的数组(字符数组排序)
*/
#include <iostream>
#include <cstdio>
using namespace std;
void bubble_sort(char a[],int n);
void output_array(char a[],int n);
int main( )
{
char a[20]={'s','o','r','t','b','u','b','b','l','e','s','e','l','e','c','t','o','k','o','k'};
char b[15]={'a','b','a','s','o','r','t','b','u','r','t','b','l','e','s'};
bubble_sort(a,20);
output_array(a,20);
cout<<endl;
bubble_sort(b,15);
output_array(b,15);
return 0;
}
void bubble_sort(char a[],int n)
{
int t;
for(int i=0; i<n; ++i)
{
for(int j=0; j<n-1; ++j)
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return;
}
void output_array(char a[],int n)
{
for(int i=0; i<n; ++i)
{
cout<<a[i]<<' ';
}
return;
}
运行结果:
本文详细介绍了冒泡排序算法的实现过程,并通过实例展示了如何使用该算法对字符数组进行排序。包括算法原理、代码实现及运行结果分析。

被折叠的 条评论
为什么被折叠?



