上机题目(初级)-整型数排序(Java)

题目如下:


代码如下:

package huawei;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public final class Demo {
	/*****************************************************************************
	 * Description : 实现整数排序,即先将从A输入的整型数序列进行排序,剔除重复整型数,输出得到的升序序列B; Input :
	 * array_A 输入参数,输入待排序整型数序列A Return : 排序后的整型数序列
	 *****************************************************************************/
	public static int[] sort(int[] array_A) {

		int count = 0;
		if (array_A == null) {
			return null;
		}
		int len = array_A.length;
		/**
		 * 冒泡法进行排序
		 */
		for (int i = len - 1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				if (array_A[j] > array_A[j + 1]) {
					int temp = array_A[j];
					array_A[j] = array_A[j + 1];
					array_A[j + 1] = temp;
				}
			}
		}
		Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
		for (int i = 0; i < len; i++) {
			if (!hm.containsValue(array_A[i])) {//有的话不进行插入
				hm.put(count, array_A[i]);
				count++;
			}
		}
		int[] output = new int[count];
		for (int i = 0; i < count; i++) {
			output[i] = hm.get(i);//存入整型数组中
		}
		return output;
	}

	public static void main(String[] args) {
		System.out.println("输入序列A:");
		Scanner cin = new Scanner(System.in);
		String input = cin.nextLine();
		String[] string = input.split(",");
		int[] inputInt = new int[string.length];
		int[] outputInt=new int[string.length];
		for (int i = 0; i < string.length; i++) {
			inputInt[i] = Integer.parseInt(string[i]);
		}
		// int[] a={76,92,34,34,59,16,59,45};
		outputInt=sort(inputInt);
		for(int i=0;i<outputInt.length;i++){
			if(i<outputInt.length-1){
			System.out.print(outputInt[i]+",");
			}else
				System.out.print(outputInt[i]);
		}
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值