第十三周项目4-数组的排序(1 冒泡排倒序)

冷不防 2022-08-14 02:41 212阅读 0赞
  1. /*
  2. *Copyright(c)2014,烟台大学计算机学院
  3. *All rights reserved.
  4. *文件名称:test.cpp
  5. *作者:满星辰
  6. *完成日期:2014年 11月 23日
  7. *版本号:v1.0
  8. *
  9. *问题描述:用冒泡法按降序排序
  10. *程序输入:
  11. *程序输出:
  12. */
  13. #include <iostream>
  14. using namespace std;
  15. void bubble_sort(int a[],int n);
  16. void output_array(int a[],int n);
  17. int main( )
  18. {
  19. int a[20]= {86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};
  20. int b[15]= {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};
  21. bubble_sort(a,20); //用冒泡法按降序排序a中元素
  22. output_array(a,20); //输出排序后的数组
  23. cout<<endl;
  24. bubble_sort(b,15); //用冒泡法按降序排序b中元素
  25. output_array(b,15); //输出排序后的数组
  26. return 0;
  27. }
  28. //请在下面定义bubble_sort和output_array函数
  29. void bubble_sort(int a[],int n)
  30. {
  31. int t;
  32. for(int i=0; i<n; ++i)
  33. {
  34. for(int j=0; j<n-1; ++j)
  35. {
  36. if(a[j]<a[j+1])
  37. {
  38. t=a[j];
  39. a[j]=a[j+1];
  40. a[j+1]=t;
  41. }
  42. }
  43. }
  44. return;
  45. }
  46. void output_array(int a[],int n)
  47. {
  48. for(int i=0; i<n; ++i)
  49. {
  50. cout<<a[i]<<' ';
  51. }
  52. return;
  53. }

运行结果:

Center

学习心得:

我觉得我的程序运行效率有点低。。。。

争取改进一下

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读