package com.duapp.itfanr;
public class CharDemo {
public static void main(String args[]) {
int input1[] = { 3, 6, 1, 9, 7 };
int input2[] = { 3, 6, 1, 9, 7, 8 };
// int len = input1.length ;
// bubbleSort(input1);
// sort(input1, len , output) ;
int len = input2.length;
int[] output = new int[len];
bubbleSort(input2);
sort(input2, len, output);
for (int i = 0; i < len; i++)
System.out.println(output[i]);
}
static void sort(int input[], int n, int output[]) {
int middle;
if (n % 2 == 1) {
middle = (n - 1) / 2;
} else {
middle = n / 2;
}
output[middle] = input[0];
int i = 1, j = 1, ii = 1;
while (true) {
if (ii == n)
break;
else {
output[middle - i] = input[ii];
i++;
ii++;
if (ii == n)
break;
else
output[middle + j] = input[ii];
j++;
ii++;
}
}
}
static void bubbleSort(int input[]) {
int len = input.length;
for (int i = 1; i < len; i++) {
for (int j = 0; j < len - i; j++) {
if (input[j] < input[j + 1]) {
int temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}
}
}
}
}
参考: [1].
http://blog.youkuaiyun.com/yuan22003/article/details/6779622
给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。 例如: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} 函数接口 void sort(int input[[, int n, int output[])
转载于:https://my.oschina.net/itfanr/blog/195653