我正在尝试根据包含 n 个随机值的数组的值对包含值 0,1,2...n 的数组进行排序
例如,随机值数组 [57,12,84,5,71] 会导致数组排序为 [3,1,0,4,2]
public void shuffle(Object[] a) {
//TODO: implement this!
if (a.length < 2) {
return;
}
int size = a.length;
Random rand = new Random();
Comparable[] randValues = new Comparable[size];
for (int i = 0; i < size; i++) {
randValues[i] = rand.nextInt(Integer.MAX_VALUE);
}
Object randIndex[] = new Object[a.length];
for (int i = 0; i < size; i++) {
randIndex[i] = i;
}
Comparable[] sortedArr = mergesort(randValues);
for (int i = 0; i < size; i++) {
int index = (int) randIndex[i];
randIndex[i] = sortedArr[index];
}
for (int i = 0; i < size; i++) {
int index = (int) randIndex[i];
a[i] = a[index];
}
}
mergesort 是一个单独的方法,参数 Comparble[] a 最终结果应该洗牌 Object[] a 的内容 当前代码导致 IndexOutofBounds 异常
你可以使用这样的方法。阅读代码中的所有注释:
/**
* Returns an int[] array of the supplied array element index values from
* the lowest element value to the highest element value. Only index values
* are returned within the provided int[] array, for example:<pre>
*
* A supplied int[] array containing:
*
* {@code int[] a = {1, 3, 2, 5, 4, 7, 8, 6, 0}; }
*
* would return an index int[] array of:
*
* {@code [8, 0, 2, 1, 4, 3, 7, 5, 6] } </pre>
*
* @param array (int[] Array) The integer array to process.<br>
*
* @return (int[] Array) Containing index values of the supplied array's
* elements in ascending order.
*/
public int[] sortedArrayOfElementIndexes(int[] array) {
// If the supplied int[] array contains nothing then return null;
if (array == null || array.length == 0) {
return null;
}
/* List Interface of Integer to hold index values of
lowest element values to highest element values. */
List<Integer> indexes = new ArrayList<>();
/* Integer variable to hold the index value of the next
lowest element value. */
int idx = 0;
/* Iterate through the array one element at a time from
left to right... */
for (int i = 0; i < array.length; i++) {
/* Set a low-value variable to an outragiously high
value. This variable will hold the next lowest
element value from the array during processing. */
int lowVal = Integer.MAX_VALUE;
/* Iterate through the array again to get the next
lowest element value. If an element index value
has already been stored then that array element
is ignored:
*/
for (int j = 0; j < array.length; j++) {
/* If the current element index value has already
been stored then ignore it: */
if (indexes.contains(j)) {
continue;
}
/* Determine the lowest element value that hasn't
already been stored: */
if (array[j] < lowVal) {
lowVal = array[j]; // Store next lowest element value.
idx = j; // Store the array index value for that element.
}
}
// Add the determined index value in the List:
indexes.add(idx);
}
// Convert List<Integer> to int[] using Stream:
int[] result = indexes.stream().mapToInt(i -> i).toArray();
// Return the result[] array:
return result;
}
要使用上面的方法,你可以这样做:
// The supplied int[] array:
int[] a = {1, 3, 2, 5, 4, 7, 8, 6, 0};
int[] result = sortedArrayOfElementIndexes(a);
// Display the result[] Array to Console Window:
System.out.println("Supplied Array: -> " + Arrays.toString(a));
System.out.println("Sorted Index Array: -> " + Arrays.toString(result));
控制台窗口的输出应该类似于:
Supplied Array: -> [1, 3, 2, 5, 4, 7, 8, 6, 0]
Sorted Index Array: -> [8, 0, 2, 1, 4, 3, 7, 5, 6]