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

时间:2020-12-07 18:52:45

题目如下:

上机题目(初级)-整型数排序(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]);
}

}

}