(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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值