# 华为笔试上机题

例如：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}

#include <string.h>

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#define MAX_NUM 6

int insert_sort(int* p, int num)
{
int j, tmp;

if(NULL==p)
{
return -1;
}

for(int i=1; i<num; i++)
{
tmp = p[i];
for(j=i; j>0; j--)
{
if(p[j-1]<tmp)
{
p[j] = p[j-1];
}
else
{
break;
}
}
p[j] = tmp;
}

return 0;
}

int show(int* p, int num)
{
if(NULL==p)
{
return -1;
}

for(int i=0; i<num; i++)
{
printf("%d ", p[i]);
}

printf("\n");
return 0;
}

int GetValue(int* input, int* output, int num)
{
int* p=NULL;
int len=num/2;
int j=0;

if(NULL==input || NULL==output)
{
return -1;
}

p=(int*)malloc(sizeof(int)*num);
if(NULL==p)
{
return -1;
}
memcpy(p, input, sizeof(int)*num);

insert_sort(p, num);

output[len] = p[j++];

for(int i=1; j<num; i++)
{
if(len-i>=0)
{
output[len-i] = p[j++];
}

if(len+i<num)
{
output[len+i] = p[j++];
}
}

free(p);
}

void main(int argc, const char * argv[]) {

int input[MAX_NUM] = {3,6,1,9,7,8};
int output[MAX_NUM] = {0};

GetValue(input, output, MAX_NUM);

show(input, MAX_NUM);
show(output, MAX_NUM);
}