数组专题讲义之简单排序算法

[cpp]  view plain   copy
  1. 专题二 简单排序算法  
  2. 阅读代码,回答下列问题:  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. const int M = 100;  
  7. const int N = 10;  
  8. int T[N+1];//桶排序专用数组(元素值为0-N的整数)  
  9.   
  10. void PrintArray(const int A[], int low, int high); //输出数组A中区域[low,high]的元素  
  11. void BubbleSort (int A[], int n); //用冒泡排序法对数组A进行排序(非递减序列)  
  12. void SelectionSort(int A[], int n); //用选择排序法对数组A进行排序(非递减序列)   
  13. void InsertSort(int A[], int n); //用插入排序法对数组A进行排序(非递减序列)   
  14. void BucketSort(int A[], int n);//用桶排序法对数组A进行排序(非递减序列)  
  15.   
  16. int main()  
  17. {  
  18.     int A[M] = {2,8,2,1,2,6,5,6};  
  19.     int B[M] = {2,8,2,1,2,6,5,6};  
  20.     int C[M] = {2,8,2,1,2,6,5,6};  
  21. int D[M] = {2,8,2,1,2,6,5,6};  
  22.     int n = 8;  
  23.       
  24.     BubbleSort(A, n);//冒泡排序算法   
  25.     PrintArray(A, 0, n-1);  
  26.     SelectionSort(B, n);//选择排序算法   
  27.     PrintArray(B, 0, n-1);  
  28.     InsertSort(C, n);//插入排序算法   
  29.     PrintArray(C, 0, n-1);  
  30.     BucketSort(D, n);//桶排序算法   
  31.     PrintArray(D, 0, n-1);  
  32.       
  33.     return 0;  
  34. }  
  35.   
  36. void PrintArray(const int A[], int low, int high) //输出数组A中区域[low,high]的元素  
  37. {  
  38.     while (low <= high)  
  39.         cout << A[low++] << " ";  
  40.     cout << endl;  
  41. }  
  42.   
  43. 算法1:用冒泡排序法对数组A进行排序(非递减序列)   
  44. void BubbleSort(int A[], int n)  
  45. {  
  46.     int temp;   
  47. for (int i=n-1; i>0; i--)   
  48.     {  
  49.         for (int j=0; j<i; j++) //通过交换相邻位置元素,进行冒泡操作  
  50.         {  
  51.             if (A[j] > A[j+1])  
  52.             {  
  53.                 temp = A[j];  
  54.                 A[j] = A[j+1];  
  55.                 A[j+1] = temp;  
  56.             }  
  57.         }  
  58.     }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值