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 ;}