第十三周 项目四--数组的排序(2)对字符排序

来源:互联网 发布:java 命令行打包 编辑:程序博客网 时间:2024/06/06 00:26

问题及代码:

/**Copyright (c) 2014,烟台大学计算机学院*All rights reserved.*文件名称:test.cpp*作者:吴胜男*完成日期:2014年11月22日*版本号:v1.0**问题描述:编写程序,完成冒泡排序。*输入描述:略*程序输出:输出冒泡法降序排序后的数组元素。*/#include <iostream>using namespace std;void bubble_sort(char sort[],int n);char output_array(char array[],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','b','b','l','e','s','e'};    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 sort[],int n){    int i,j;    char t;    for(j=1; j<=n-1; j++)        for(i=0; i<n-j-1; i++)            if(sort[i]<sort[i+1])            {                t=sort[i];                sort[i]=sort[i+1];                sort[i+1]=t;                sort[i]=char(sort[i]);            }    return;}char output_array(char array[],int n){    int i;    for(i=0; i<n; i++)        cout<<array[i]<<" ";    cout<<endl;    return array[i];}


运行结果:

知识点总结:字符型大小比较,直接比即可。n个数字,n-1轮比较,每轮n-j次比较。

学习心得:灵活处理一些问题!

0 0