C + + arranges integer array elements _c language according to normal distribution

Source: Internet
Author: User
Tags array length

The subject requirements are as follows:

Given an array of input[],

If the array length n is odd, the largest element in the array is placed in the middle of the output[] array,

If the array length n is an even number, the largest element in the array is placed at the right position in the middle of the output[] array two positions,

Then, in order from large to small, sequentially in the first position on both sides, according to the order of the left and right, in turn, the remaining number.

The result of this processing is that if you represent a distributed graph according to the value of the element, the drawing should be a normal distribution.

About normal Distribution:

The normal distribution, also known as the Gaussian distribution (Gaussian distribution), is a very important probability distribution in the fields of mathematics, physics and engineering, Have a great influence on many aspects of statistics. If the random variable x obeys a Gaussian distribution with a mathematical expectation of μ and variance as σ^2, it is recorded as N (μ,σ^2). The expectation μ of the probability density function is the normal distribution, and its standard deviation σ determines the amplitude of distribution. Because the curve is bell-shaped, so people often call it the bell curve. What we normally call the standard normal distribution is the normal distribution of μ= 0,σ= 1.

There is only a connection from the result, and the algorithm has nothing to do with the normal distribution.

The code implementation is as follows:

void sort (int input[],int output[], int n) {int m=n; 
   
  cout<<m<<endl; 
  int i,j,temp; BOOL exchange;//Exchange flag for (i=0;i<m-1;i++) {//up to do n-1 trip sort exchange=false;//The Exchange flag should be a false for (j=m-2;j> =i;j--)//to the current unordered area r[i ... N] from the bottom up scan if (Input[j+1]<input[j]) {//Exchange records TEMP=INPUT[J+1];//r[0] is not a sentinel, only a staging unit input[j+1]= 
        INPUT[J]; 
        Input[j]=temp; Exchange=true; 
      There is an exchange, so the Exchange flag is set to True} if (!exchange)//This trip sort does not occur exchange, the early termination algorithm break; 
  cout<<input[5]<<endl; 
  for (int wc1=0; wc1<m; wc1++)//Just to show the sort result ~ {cout<<input[wc1]<< ""; 
   
  } cout << Endl; 
  int q=m-1; 
    if ((m%2) ==0) {int mid=m/2; 
      for (int tempmid=0; tempmid<=mid; tempmid++)//Note the execution order of the circular statement {OUTPUT[MID+TEMPMID]=INPUT[Q]; 
      q--; 
      OUTPUT[MID-TEMPMID-1]=INPUT[Q]; 
    q--; 
    } if ((m%2)!=0)//Note the execution order of the loop statement {int MID=Q/2; OutPUT[MID]=INPUT[Q]; 
      for (int tempmid=1;tempmid<=mid;tempmid++) {q--; 
      OUTPUT[MID-TEMPMID]=INPUT[Q]; 
      q--; 
    OUTPUT[MID+TEMPMID]=INPUT[Q]; 
  for (int wc=0; wc<m; wc++) {cout<<output[wc]<< "; 
} cout << Endl; 

 }
int main () 
{ 
   int input[] = {3, 6, 1, 9, 7, 8, 2}; 
   int wc=0; 
   int ncount = sizeof (input)/sizeof (int); 
   For (wc=0 wc<ncount; wc++)// 
  { 
    COUT<<INPUT[WC] << ""; 
    cout<< "\ n" <<endl; 
  } 
  cout << Endl; 
 
  int output[]= {3, 6, 1, 9, 7, 8, 2}; 
  Sort (input,output, ncount); 
   
  return 0; 
} 

Test results:

when int input[] = {3, 6, 1, 9, 7, 8, 2, 10};, the result is as follows:

3 6 1 9 7 8 2 10

1 2 3 6 7 8 9 10

1 3 7 9 10 8 6 2

when int input[] = {3, 6, 1, 9, 7, 8, 2, 10};, the result is as follows:

3 6 1 9 7 8 2

1 2 3 6 7 8 9

2 6 8 9 7 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.