1. 程式人生 > >(PAT)Insertion or Heap Sort(堆排序與插入排序)

(PAT)Insertion or Heap Sort(堆排序與插入排序)

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.

Heap sort

 divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 "Heap 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 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

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

解題思路:

只需在每一次排序後,將部分排序序列和排序序列的每一個元素進行比較,如果在若干次排序後,部分排序序列與該排序序列相等,那麼就是插入排序或者堆排序

首先判斷是否是插入排序

bool isSame(int a[], int b[]) {
	for (int i = 1; i <= n; ++i) {
		if (a[i] != b[i]) {
			return false;
		}
	}
	return true;
}

/*插入排序序列*/
bool isInsertSort() {
	bool flag = false;
	for (int i = 2; i <= n; ++i) {
		if (i != 2 && isSame(origin, midSort)) {
			flag = true;
		}
		sort(origin, origin + i + 1);
		if (flag == true) {
			cout << "Insertion Sort" << endl;
			for (int j = 1; j <= n; ++j) {
				if (j == n) {
					cout << origin[j];
				}
				else {
					cout << origin[j] << " ";
				}
			}
			return true;
		}
	}
	return false;
}

如果是插入排序,則直接輸出,如果不是,進一步判斷是否是堆排序

我們對序列建堆,然後進行堆排序

/*堆排序序列*/
void downAdjust(int low, int high) {   //low表示最低的元素下標,high表示陣列的最後一個元素的下標
	int current = low, lchild = current * 2;   //j表示左孩子
	while (lchild <= high) {       //如果左孩子存在
		//如果右孩子存在,且右孩子的值大於當前結點值
		if (lchild + 1 <= high && heap[lchild] < heap[lchild + 1]) {
			lchild = lchild + 1;   //改成右節點
		}

		if (heap[lchild] > heap[current]) {
			swap(heap[lchild], heap[current]);
			current = lchild;
			lchild = current * 2;
		}
		else {
			break;
		}
	}
}

void createHeap() {  //建立堆:把堆進行排序
	for (int i = n / 2; i >= 1; i--) {   //
		downAdjust(i, n);
	}
}

void insert(int x) {
	heap[++n] = x;
	int current = n;
	int father = n / 2;
	while (father >= 1 && heap[current] > heap[father]) {
		swap(heap[current], heap[father]);
		current = father;
		father = current / 2;
	}
}

void traverse() {
	for (int i = 1; i <= n; ++i) {
		cout << heap[i] << " ";
	}
	cout << endl;
}

void heapSort() {
	for (int i = n; i > 1; --i) {
		bool flag = true;
		swap(heap[1], heap[i]);
		downAdjust(1, i - 1);
		/*在這裡進行比較*/
		for (int j = 1; j <= n; ++j) {
			if (midSort[j] != heap[j]) {
				flag = false;
				break;
			}
		}
		if (flag == true) {
			cout << "HeapSort" << endl;
			swap(heap[1], heap[i-1]);
			downAdjust(1, i - 2);
			for (int i = 1; i <= n; ++i) {
				if (i == n) {
					cout << heap[i];
				}
				else {
					cout << heap[i] << " ";
				}
			}
		}
	}
}

主函式內容如下:

int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> heap[i];
		origin[i] = heap[i];
	}
	
	for (int i = 1; i <= n; ++i) {
		cin >> midSort[i];
	}

	bool isinsr = isInsertSort();
	if (isinsr == false) {
		createHeap();
		heapSort();
	}

	system("PAUSE");
	return 0;
}