这是用于创建算法的说明。
首先将给定 X 的所有 Y 组装到一个 Map 中。然后将给定 Y 的所有 X 组合起来。这样您最终将得到数据结构 Map<Key,Value> 为 Map<Integer,List<Integer>>。例如,如果 Q 是
1 2
2 3
1 3
4 3
那么对于 X=1,您将有一个 Map 条目为 <1,{2,3}>。所以完整的映射将是
<1,{2,3}>
<2,{1,3}>
<4,{3}>
<3,{1,2,4}>
请注意,由于 Q 是一组可交换操作,我们不需要为 2 提供两个键,尽管两个同时显示为 X 和 Y。所以我希望你能够遵循到目前为止,因为整个算法都休息在这个映射上。然后是算法。
package algorithm;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class SortArray {
/**
* Given an array and a set of commutative and transitive exchanges, sort the array in
* increasing order. return the number of operations it took to sort the array.
*
* @param input
* @param operations
* @return
*/
static int sortArray(int[] input, int[][] operations) {
int ops = 0;
Map<Integer, Set<Integer>> map = flattenOperations(operations);
int min = smallestElement(operations);
for (int i = 0; i < input.length; i++) {
ops += minimize(i, input, min, map);
}
if (isSortedAscending(input)) {
return ops;
} else {
return -1;
}
}
/**
* the given array is a set of {X,Y} pairs as in {{X0,Y0},{X1,Y1},...}.
* This method assembles all the Xs for a given Y and then all the Ys for a given X.
* This is okay because the operations are commutative.
*
* @param operations
* @return
*/
static private Map<Integer, Set<Integer>> flattenOperations(int[][] operations) {
Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
// add Ys for X keys
for (int[] operation : operations) {
Set<Integer> s = map.get(operation[0]);
if (s == null) {
s = new TreeSet<Integer>();
s.add(operation[1]);
map.put(operation[0], s);
} else {
s.add(operation[1]);
}
}
for (int[] operation : operations) {
Set<Integer> s = map.get(operation[1]);
if (s == null) {
s = new HashSet<Integer>();
s.add(operation[0]);
map.put(operation[1], s);
} else {
s.add(operation[0]);
}
}
return map;
}
static private int smallestElement(int[][] operations) {
int min = Math.min(operations[0][0], operations[0][1]);
for (int i = 1; i < operations.length; i++) {
int tmpMin = Math.min(operations[i][0], operations[i][1]);
min = Math.min(min, tmpMin);
}
return min;
}
/**
* Replace the element at the given index i with the minimum value possible. The minimum value
* possible is greater than or equal to min but less than the original value at input[i]. If
* the operation was successful return the number of exchanges, else return 0. This is a
* subroutine. It makes sense in the greater context. This may not end in a sorted array. But
* the caller is responsible for checking for sorting at a later point.
*
*
* @param i
* @param input
* @param min
* @param map
* @return
*/
static private int minimize(int i, int[] input, int min, Map<Integer, Set<Integer>> map) {
int key = input[i];
boolean done = false;
int ops = 0;
while (!done) {
int tmp = findSmallestValueLessThanKeyButGreaterThanOrEqualToMin(key, min, map);
if (-1 != tmp) {
key = tmp;
ops++;
} else {
done = true;
}
}
input[i] = key;
return ops;
}
/**
* The lists are assumed to be pre-sorted in ascending order so that the smallest value is at
* index 0.
*
* @param key
* @param min
* @param map
* @return
*/
static private int findSmallestValueLessThanKeyButGreaterThanOrEqualToMin(int key, int min, Map<Integer, Set<Integer>> map) {
Set<Integer> set = map.get(key);
if (null != set) {
for (int el : set) {
if (el < key && el >= min) {
return el;
}
}
}
return -1;
}
static private boolean isSortedAscending(int[] input) {
for (int i = 1; i < input.length; i++) {
if (input[i - 1] > input[i]) {
return false;
}
}
return true;
}
// ====================================================================================
public static void main(String[] args) {
// int[][] Q = { { 1, 2 }, { 2, 3 }, { 1, 3 } };
// int[] input = { 3, 2, 1 };
int[][] Q = { { 2, 3 } };
int[] input = { 2, 1 };
int ops = sortArray(input, Q);
System.out.println("Number of operations is " + ops);
System.out.println("The new array is " + Arrays.toString(input));
}
}