Obtain sequence as required

Source: Internet
Author: User

Question:

Given an array input [], if the array length N is an odd number, place the largest element in the array in the middle of the output [] array. If the array length N is an even number, place the largest element in the array to the right of the two locations in the middle of the output [] array, and then place the elements in the order of size to size on both sides of the first position in sequence, store the remaining number in the order of one left and one right.

Example: input [] = {3, 6, 1, 9, 7} output [] = {3, 7, 9, 6, 1 };

Input [] = {3, 6, 1, 9, 7, 8} output [] = {1, 6, 8, 9, 7, 3}

Function interface void sort (INT input [], int N, int output [])

 

# Include <iostream> Using   Namespace  STD;  Void Sort ( Int Input [], Int N,Int  Output []);  Int  Main (){  Int Input [] = { 3 , 6 , 1 , 9 , 7  };  Int Output [ 5  ]; Sort (input,  5  , Output ); Int Input2 [] = { 3 , 6 , 1 , 9 , 7 , 8  };  Int Output2 [ 6  ]; Sort (input2,  6  , Output2 );  Return   0 ;}  Void Sort ( Int Input [], Int N, Int  Output []) {  //  Sort by selection method (from large to small)      For ( Int I = 0 ; I <n; I ++ ){  For ( Int J = I + 1 ; J <n; j ++ ){  If (Input [I] < Input [J]) {  Int TMP = Input [I]; input [I] = Input [J]; input [J] = TMP ;}}}  For ( Int I = 0 ; I <n; I ++ ) {Cout <Input [I] <"  ,  "  ;} Cout < Endl;  Int Mid; //  Maximum storage location      If (N % 2 ! = 0 ) //  Odd  {Mid = (N- 1 )/2  ;}  Else  //  Even  {Mid = N/ 2  ;}  Int K = 1  ; Output [Mid] = Input [ 0  ];  For ( Int I = 1 ; I <n; I ++ ) {K = (I + 1 )/ 2  ;  If (I % 2 ! = 0  ) {Output [Mid -K] = Input [I];}  Else  {Output [Mid + K] = Input [I] ;}}  For (Int I = 0 ; I <n; I ++ ) {Cout <Output [I] < "  ,  "  ;} Cout < Endl ;} 

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.