題目
根據維基百科的定義:
插入排序是迭代算法,逐一獲得輸入數據,逐步產生有序的輸出序列。每步迭代中,算法從輸入序列中取出一元素,將之插入有序序列中正確的位置。如此迭代直到全部元素有序。
歸併排序進行如下迭代操作:首先將原始序列看成 N 個只包含 1 個元素的有序子序列,然後每次迭代歸併兩個相鄰的有序子序列,直到最後只剩下 1 個有序的序列。
現給定原始序列和由某排序算法產生的中間序列,請你判斷該算法究竟是哪種排序算法?
輸入輸出
輸入格式:
輸入在第一行給出正整數 N (≤100);隨後一行給出原始序列的 N 個整數;最後一行給出由某排序算法產生的中間序列。這裏假設排序的目標序列是升序。數字間以空格分隔。
輸出格式:
首先在第 1 行中輸出Insertion Sort
表示插入排序、或Merge Sort
表示歸併排序;然後在第 2 行中輸出用該排序算法再迭代一輪的結果序列。題目保證每組測試的結果是唯一的。數字間以空格分隔,且行首尾不得有多餘空格。
輸入樣例 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
輸出樣例 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
輸入樣例 2:
10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6
輸出樣例 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
題目信息
作者:CHEN, Yue
單位:浙江大學
代碼長度限制:16 KB
時間限制:200 ms
內存限制:64 MB
題目知識點
題目分析
把 原始序列存爲A,把第二個序列存爲 B,分別對原始序列進行排序,在排序過程中看哪個排序方式過程中出現了序列B,就輸出哪個序列,並用該方法再進行一輪排序
注意點
①:歸併排序的步長 和 插入排序排到了第幾個數 都要設置成靜態,這是爲了方便確定了排序方法之後在進行一輪排序是,能夠繼續按照之前的狀態進行
②:由於Java的數組是引用類型,所以函數中數組發生了改變就會改變原數組,因此兩種排序不可以用同一個數組
③:每一輪排序中都要判斷一下兩個序列是否相等,如果想等就返回。但是不可直接返回,因爲之後還要再進行一輪排序,直接返回的話下次排序還是停留在這一輪,所以要把對應排序的初始狀態重置到下一輪
④:不知道爲什麼,我提交的時候PAT不支持 Arrays.compare(),所以需要專門寫一個函數來比較兩個數組是否相等
Java代碼
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
class Main{
public static int[] B;
public static int step=2; // 注意點①
public static int insertCount = 1; // 注意點①
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String[] strArr = bf.readLine().split(" ");
int[] A = new int[strArr.length];
B = new int[strArr.length];
for(int i=0; i<A.length; i++) {
A[i] = Integer.parseInt(strArr[i]);
}
strArr = bf.readLine().split(" ");
for(int i=0; i<B.length; i++) {
B[i] = Integer.parseInt(strArr[i]);
}
int[] C = A.clone(); // 注意點②
boolean isMerge = MergeSort(A, false);
InsertionSort(C, false);
if(isMerge) {
System.out.println("Merge Sort");
MergeSort(A, true);
}else {
System.out.println("Insertion Sort");
InsertionSort(C, true);
A = C;
}
for(int i=0; i<A.length-1; i++) {
System.out.print(A[i]+" ");
}
System.out.println(A[A.length - 1]);
}
//合併數組的兩端位置 相鄰
public static void merge(int[] A, int left1, int right1, int left2, int right2) {
int i = left1, j = left2;
int[] temp = new int[right2 - left1 + 1];
int index = 0;
while(i <= right1 && j <= right2) {
if(A[i] <= A[j]) {
temp[index++] = A[i++];
}else {
temp[index++] = A[j++];
}
}
while(i <= right1)
temp[index++] = A[i++];
while(j <= right2)
temp[index++] = A[j++];
for(int i1=0; i1<temp.length; i1++) {
A[left1 + i1] = temp[i1];
}
}
// 歸併排序 返回比較結果
public static boolean MergeSort(int[] A, boolean onceTag) {
for(; step/2<=A.length; step*=2) {
for(int i=0; i<A.length; i+=step) {
int mid = i - 1 + step/2;
if(mid < A.length - 1){ // 判斷右子區間是否有元素,有就合併
// 左子區間[i, mid], 右子區間爲[mid+1,Math.min(i+step, n)] (這是爲了防止超過步長)
merge(A, i, mid, mid+1, Math.min(i - 1 + step, A.length-1));
}
}
if(onceTag == true)
break;
if( compare(A, B) == 0 ) {
step *= 2; // 注意點③
return true;
}
}
return false;
}
// 插入排序
public static boolean InsertionSort(int[] A, boolean onceTage) {
for(; insertCount < A.length; insertCount++) {
int temp = A[insertCount], j=insertCount;
while(j>0 && A[j-1] > temp) {
A[j] = A[j-1];
j--;
}
A[j] = temp;
if(onceTage == true)
return false;
if(compare(A, B)==0) {
insertCount++; //注意點③
return true;
}
}
return false;
}
// 注意點④
public static int compare(int[] A, int[] B) {
for(int i=0; i<A.length; i++) {
if(A[i] != B[i])
return -1;
}
return 0;
}
}