Java implements full arrangement of array elements

Question: Perform a full permutation of the sequence. For example, the full permutation of 1 2 3 has 1 2 3 , 1 3 2, 2 1 3, ... a total of 6 different sorting methods, and the sorting result of an array of n elements is n! different sorting results. Implement this function

import java.util.ArrayList;
import java.util.Arrays;

/*
 * This method uses recursive calls, the complexity is n!, to be optimized
 */
public class Main {
	public static void main(String[] args) {
		//Construct an array and realize its full arrangement
		String[] srcArr = { "a", "b", "c", "d" };
		//result is the result of the full arrangement of the above array. It stores n! elements in the form of an array list, and each element is an array in an arrangement order.
		ArrayList<String[]> result = testArrange(srcArr);
		// print the result,
		while (!result.isEmpty()) {
			System.out.println(Arrays.toString(result.get(0)));
			result.remove(0);
		}
	}
/*
 * Full permutation recursive method, for an array of n elements, place each element in the first place, and the remaining order remains unchanged to construct an array of n-1 elements, and call this method recursively
 * Until there are only 2 elements, swap the order, return
 */
	public static ArrayList<String[]> testArrange(String[] srcArr) {
		ArrayList<String[]> result = new ArrayList<>();
		int length = srcArr.length;
		//If there are only two elements, then there are only two permutations, the original order and the exchanged order
		if (length == 2) {
			result.add(srcArr);
			result.add(exchangeElement(srcArr, 0, 1));
		} else {
			//From the first element to the last element, take it out in turn and place it in the first position,
// Other elements recursively call this method in the original order until there are only two elements
			for (int i = 0; i < length; i++) {
				String[] before = new String[1];
// before is used to store the first element,
				before[0] = srcArr[i];
				String[] tmp = new String[length - 1];
// tmp is the remaining array after the original array is taken out of the i-th element and placed in the first position
				tmp = reduceArr(srcArr, i);
				//Recursive for the remaining part to achieve full permutation
				ArrayList<String[]> afterList = testArrange(tmp);
				while (!afterList.isEmpty()) {
// Concatenate the first element and the rest of the full arrangement
					result.add(appendArr(before, afterList.get(0)));
					afterList.remove(0);
				}
			}
		}
		return result;
	}
/*
 * Swap the values ​​of i, and j elements in the array
 */
	public static String[] exchangeElement(String[] srcArr, int i, int j) {
		String[] result = Arrays.copyOf(srcArr, srcArr.length);
		result[i] = srcArr[j];
		result[j] = srcArr[i];
		return result;
	}
/*
 * Delete the element at index position in the array, the order of other elements remains unchanged
 */
	public static String[] reduceArr(String[] srcArr, int index) {
// Deleted array, length -1
		String[] result = new String[srcArr.length - 1];
		for (int i = 0, j = 0; i < result.length; i++, j++) {
			if (i == index)
				//The element at the index position, skip the copy, copy the next element
				j++;
			result[i] = srcArr[j];
		}
		return result;
	}
//Concatenate the two arrays, the first parameter is in front, and the second parameter array returns an array after
	public static String[] appendArr(String[] before, String[] after) {
		String[] result = new String[before.length + after.length];
		for (int i = 0, j = 0; i < result.length; i++) {
			if (i < before.length) {
				result[i] = before[i];
			} else {
				result[i] = after[j];
				j++;
			}
		}
		return result;
	}

}

The result is:

[a, b, c, d]
[a, b, d, c]
[a, c, b, d]
[a, c, d, b]
[a, d, b, c]
[a, d, c, b]
[b, a, c, d]
[b, a, d, c]
[b, c, a, d]
[b, c, d, a]
[b, d, a, c]
[b, d, c, a]
[c, a, b, d]
[c, a, d, b]
[c, b, a, d]
[c, b, d, a]
[c, d, a, b]
[c, d, b, a]
[d, a, b, c]
[d, a, c, b]
[d, b, a, c]
[d, b, c, a]
[d, c, a, b]
[d, c, b, a]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325611184&siteId=291194637