package class06; import java.util.Arrays; import java.util.PriorityQueue; /** * sortedArrDistanceLessK() * 已知一个几乎有序的数组。几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离一定不超过k,并且k相对于数组长度来说是比较小的。 * 请选择一个合适的排序策略,对这个数组进行排序。 */ public class Code04_SortArrDistanceLessK { public static void sortedArrDistanceLessK(int[] arr, int k) { // arr = new int[]{2, 1, 8, 4, 9, 3, 5}; if (k == 0) { return; } PriorityQueue<Integer> heap = new PriorityQueue<>(); int index = 0; for (; index <= Math.min(arr.length - 1, k - 1); index++) {//=号不能少,如果不写=号,会导致排序错误。 heap.add(arr[index]); } int i = 0; for (; index < arr.length; i++, index++) { heap.add(arr[index]); arr[i] = heap.poll(); } while (!heap.isEmpty()) { arr[i++] = heap.poll(); } } public static void sortedArrDistanceLessK111(int[] arr, int k) { if (k == 0) { return; } // 默认小根堆 PriorityQueue<Integer> heap = new PriorityQueue<>(); int index = 0; // 0...K-1 for (; index <= Math.min(arr.length - 1, k - 1); index++) { heap.add(arr[index]); } int i = 0; for (; index < arr.length; i++, index++) { heap.add(arr[index]); arr[i] = heap.poll(); } while (!heap.isEmpty()) { arr[i++] = heap.poll(); } } public static int[] randomArrayNoMoveMoreK(int maxSize, int maxValue, int K) { int[] arr = new int[(int) (Math.random() * (maxSize + 1))]; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * maxValue); } Arrays.sort(arr); boolean[] isSwapped = new boolean[arr.length]; for (int i = 0; i < arr.length; i++) { // int j = (int) (Math.random() * (Math.min(arr.length - 1, K - 1))); int j = Math.min(i + (int) (Math.random() * (K + 1)), arr.length - 1); if (!isSwapped[i] && !isSwapped[j]) { isSwapped[i] = true; isSwapped[j] = true; swap(arr, i, j); } } return arr; } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int[] copyArray(int[] arr) { if (arr == null) { return null; } int[] res = new int[arr.length]; for (int i = 0; i < arr.length; i++) { res[i] = arr[i]; } return res; } // for test public static boolean isEqual(int[] arr1, int[] arr2) { if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) { return false; } if (arr1 == null && arr2 == null) { return true; } if (arr1.length != arr2.length) { return false; } for (int i = 0; i < arr1.length; i++) { if (arr1[i] != arr2[i]) { return false; } } return true; } // for test public static void printArray(int[] arr) { if (arr == null) { return; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static void main(String[] args) { int maxSize = 100; int maxValue = 100; int testTimes = 100000; System.out.println("test start!"); for (int i = 0; i < testTimes; i++) { int k = (int) (Math.random() * maxSize) + 1; //int k = 3; int[] arr0 = randomArrayNoMoveMoreK(maxSize, maxValue, k); int[] arr1 = copyArray(arr0); int[] arr2 = copyArray(arr0); sortedArrDistanceLessK(arr1, k); Arrays.sort(arr2); if (!isEqual(arr1, arr2)) { System.out.println("oops!"); System.out.println("k = " + k); System.out.println("arr0 = " + Arrays.toString(arr0)); System.out.println("arr1 = " + Arrays.toString(arr1)); System.out.println("arr2 = " + Arrays.toString(arr2)); break; } } System.out.println("test end!"); } }
标签:index,sortedArrDistanceLessK,数组,int,PriorityQueue,有序,排序 From: https://www.cnblogs.com/TheFloorIsNotTooHot/p/17016622.html