项目要求:
对字符数组进行ASC码值降序处理,进行冒泡排序。
问题及代码:
#include <iostream>
using namespace std;
void bubble_sort(char s[],int num);
void output_array(char s[],int num);
//两个函数bubble_sort和output_array的声明
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','r','c','q','d','o','e','p','f','s','g','t','h','u'};
bubble_sort(a,20); //用冒泡法按降序排序a中元素
output_array(a,20); //输出排序后的数组
bubble_sort(b,15); //用冒泡法按降序排序b中元素
output_array(b,15); //输出排序后的数组
return 0;
}
//请在下面定义bubble_sort和output_array函数
void bubble_sort(char s[],int num)
{
for (int a=0; a<num-1; a++)
{
char t;
for (int b=0; b<num-a-1; b++)
{
if (s[b]<=s[b+1])
{
t=s[b];
s[b]=s[b+1];
s[b+1]=t;
}
}
}
}
void output_array(char s[],int num)
{
for (int a=0;a<num;a++)
cout<<s[a]<<" ";
cout<<endl;
}
运行结果: