java 华为机试题目-数组处理

给定一个数组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[])
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.csdn.net/yuan22003/article/details/6779622

转载于:https://my.oschina.net/itfanr/blog/358434

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值