PAT 1089 Insert or Merge

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

先模擬插入排序過程,若過程中序列與目標序列一致,則使用的是插入排序,將下一步的結果輸出;否則是歸併排序,模擬歸併排序的過程,發現與目標序列一致後,將下一步執行結果輸出。歸併排序使用迭代方法,合併時可直接用sort方法。

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

	static int[] origin, temp, changed;// 原始數組,中間數組,目標數組
	static int N;

	static void showArray() {
		for (int i = 0; i < N; i++) {
			System.out.print(temp[i]);
			if (i < N - 1)
				System.out.print(" ");
		}
	}

	static boolean isSame() {// 判斷數組是否相同
		for (int i = 0; i < N; i++) {
			if (temp[i] != changed[i])
				return false;
		}
		return true;
	}

	static boolean insertSort() {
		boolean flag = false;
		for (int i = 1; i < N; i++) {
			if (i > 1 && isSame()) {// 判斷非初始序列是否與目標相同
				flag = true;
			}
			// 插入部分
			int j = i, t = temp[i];
			for (; j > 0 && temp[j - 1] > t; j--) {
				temp[j] = temp[j - 1];
			}
			temp[j] = t;

			if (flag) {// 已經到達目標數組
				return true;
			}
		}
		return false;
	}

	static void mergeSort() {
		boolean flag = false;
		// 歸併
		for (int step = 2; step / 2 < N; step *= 2) {
			if (step > 2 && isSame()) {
				flag = true;
			}
			for (int i = 0; i < N; i += step) {// 分段排序
				Arrays.sort(temp, i, Math.min(N, i + step));
			}
			if (flag)
				return;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		N = Integer.valueOf(reader.readLine());
		origin = new int[N];
		temp = new int[N];
		changed = new int[N];
		// 原序列
		String[] line = reader.readLine().split(" ");
		for (int i = 0; i < N; i++) {
			origin[i] = Integer.valueOf(line[i]);
			temp[i] = origin[i];
		}
		// 目標序列
		line = reader.readLine().split(" ");
		for (int i = 0; i < N; i++)
			changed[i] = Integer.valueOf(line[i]);

		if (insertSort()) {
			System.out.println("Insertion Sort");
			showArray();
		} else {
			System.out.println("Merge Sort");
			// 還原
			for (int i = 0; i < N; i++) {
				temp[i] = origin[i];
			}
			mergeSort();
			showArray();
		}

	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章