去重:Set的特性+排序:partition思想
明明的随机数
http://www.nowcoder.com/questionTerminal/3245215fffb84b7b81285493eae92ff0
接受完数组后,用不可有重复元素的Set接收数组,转换成数组后利用快排的思想排序数组。
import java.util.*; public class Main{ private int[] sortList; public static void main(String[] args){ int num=0; Scanner input = new Scanner(System.in); while(input.hasNextInt()){ Set<Integer> depSet = new HashSet<>(); Main m = new Main(); num=input.nextInt(); for (int i=0; i<num; i++){ depSet.add(input.nextInt()); } Integer[] temp = depSet.toArray(new Integer[] {}); m.sortList = new int[temp.length]; for (int i=0; i<temp.length; i++){ m.sortList[i] = temp[i].intValue(); } m.recQuickSort(0, m.sortList.length-1); for (int i : m.sortList) System.out.println(i); } } protected void swap(int dex1, int dex2){ int temp = sortList[dex1]; sortList[dex1] = sortList[dex2]; sortList[dex2] = temp; } protected int partition(int leftPtr, int rightPtr, int pivot){ int right = rightPtr; while(true){ while(leftPtr < rightPtr && sortList[leftPtr] <= pivot) leftPtr++; while(leftPtr < rightPtr && sortList[rightPtr] >= pivot) rightPtr--; if (leftPtr >= rightPtr) break; else swap(leftPtr, rightPtr); } swap(leftPtr, right); return leftPtr; } protected void recQuickSort(int left , int right){ if(left >= right) return; else{ int pivot = sortList[right]; int partition = partition(left, right, pivot); recQuickSort(left, partition-1); recQuickSort(partition+1, right); } } }