array shuffling

Suppose we have an array a1, a2, ..., an, b1, b2, ..., bn. Implement an algorithm to change this array to a1, b1, a2, b2, ..., an, bn.

public void shuffle(char[] arr) {
	if (arr == null || arr.length <= 2) return;
	int end = arr.length / 2;
	for (int i = 0; i < arr.length / 2 - 1; i++) {
		int start = 2 * i + 1;
		char temp = arr[end];
		for (int j = end - 1; j >= start; j--) {
			arr[j + 1] = arr[j]; 
		}
		arr[start] = temp;
		end++;
	}
}


你可能感兴趣的:(array shuffling)