[2012] array (2)

Source: Internet
Author: User

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.
For 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 <algorithm> using namespace STD; bool CMP (int A, int B) {return (A> B);} void sort (INT input [], int N, int output []) {/* int small; // bubble sort method for (INT I = 1; I <n; I ++) {for (Int J = 0; j <n-I; j ++) {If (input [J] <input [J + 1]) {int temp = input [J]; input [J] = input [J + 1]; input [J + 1] = temp ;}} */sort (input, input + N, CMP ); // if the function name you write is sort, you can also use the sort function for (INT I = 0; I <N; I ++) cout <input [I] <"; int mid = n/2; Output [Mid] = input [0]; int IBIG = mid-1, ismall = Mid + 1; for (INT I = 1; I <n;) {If (IBIG> = 0) Output [IBIG --] = input [I ++]; if (ismall <n) Output [ismall ++] = input [I ++] ;}}

 

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.