数组排序 最大放中间

给定一个数组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[])

public class SortArry {

	public static void main(String[] args) {
		int input[] = {3,6,1,9,7};
		int output[] = new int[6];
		SortArry sortArry = new SortArry();
		sortArry.sort(input, 5, output);
		for(int i = 0; i< 5; i++)
		{
			System.out.println(output[i]);
		}
	}
	private void sort(int input[],int n,int output[])
	{
		//对数组排序 从大到小
		if (input == null)
		{
			System.out.println("数组为空");
			return ;
		}
		bubblesort(input,n);
		int low = n/2 - 1;
		int high = n/2 + 1;
		output[n/2] = input[0]; 
		for(int i = 1; i < n; )
		{
			output[low] = input[i];
			i++;
			low--;
			if(i==n)break;
			output[high] = input[i];
			i++;
			high++;
		}
		
	}
	private void bubblesort(int input[],int n ) {
		int temp = 0;
		for(int i = 0; i < n-1; i++)
		{
			for(int j = n-1; j > i; j--)
			{
				if(input[j] > input[j-1])
				{
					temp = input[j];
					input[j] = input[j-1];
					input[j-1] = temp;
				}
			}
		}
		for(int k =0; k < n; k++)
		{
			System.out.println(input[k]);
		}
			}

}

  

原文地址:https://www.cnblogs.com/newcoder/p/5757489.html