[C++] C++按照正态分布来排列整型数组元素 →→→→→进入此内容的聊天室

来自 , 2020-12-11, 写在 C++, 查看 161 次.
URL http://www.code666.cn/view/d1d5923f
  1. void sort(int input[],int output[], int n)
  2. {
  3.     int m=n;
  4.     //cout<<m<<endl;
  5.        
  6.     int i,j,temp;
  7.     bool exchange;//交换标志
  8.     for(i=0;i<m-1;i++)
  9.     { //最多做n-1趟排序
  10.         exchange=FALSE; //本趟排序开始前,交换标志应为假
  11.         for(j=m-2;j>=i;j--) //对当前无序区R[i..n]自下向上扫描
  12.                         if(input[j+1]<input[j])
  13.                         {//交换记录
  14.                                 temp=input[j+1]; //R[0]不是哨兵,仅做暂存单元
  15.                                 input[j+1]=input[j];
  16.                                 input[j]=temp;
  17.                                 exchange=TRUE; //发生了交换,故将交换标志置为真
  18.                         }
  19.                         if(!exchange) //本趟排序未发生交换,提前终止算法
  20.                                 break;
  21.                         //cout<<input[5]<<endl;
  22.         }
  23.        
  24.     for(int wc1=0; wc1<m; wc1++)//只是来显示排序结果~
  25.         {
  26.                 cout<<input[wc1]<<" ";
  27.         }
  28.         cout << endl;
  29.        
  30.         int q=m-1;
  31.         if((m%2)==0)
  32.         {
  33.                 int mid=m/2;
  34.                 for (int tempmid=0; tempmid<=mid; tempmid++)//注意循环语句的执行顺序
  35.                 {
  36.                         output[mid+tempmid]=input[q];
  37.                         q--;
  38.                         output[mid-tempmid-1]=input[q];
  39.                         q--;
  40.                 }
  41.     }
  42.        
  43.     if((m%2)!=0)//注意循环语句的执行顺序
  44.     {
  45.                 int mid=q/2;
  46.                 output[mid]=input[q];
  47.                 for (int tempmid=1;tempmid<=mid;tempmid++)
  48.                 {
  49.                         q--;
  50.                         output[mid-tempmid]=input[q];
  51.                         q--;
  52.                         output[mid+tempmid]=input[q];
  53.                 }
  54.     }
  55.        
  56.     for(int wc=0; wc<m; wc++)
  57.         {
  58.                 cout<<output[wc]<<" ";
  59.     }
  60.         cout << endl;
  61. }
  62.  
  63.  
  64. //cpp/8761

回复 "C++按照正态分布来排列整型数组元素"

这儿你可以回复上面这条便签

captcha