数据结构实验八

一.插入排序

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int main()
{
	int x;
	int cnt = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++cnt] = x;
	}
	cin >> x;
	for (int i = 2; i <= x + 1; i++) {
		a[0] = a[i];
		int j = i - 1;
		while (a[0] < a[j]) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = a[0];
	}
	for (int i = 1; i <= cnt; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

  二.冒泡排序

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int main()
{
	int x;
	int n = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++n] = x;
	}
	cin >> x;
	int k;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= n - i; j++) {
			if (a[j] > a[j + 1]) {
				k = a[j];
				a[j] = a[j + 1];
				a[j + 1] = k;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

三.希尔排序

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int main()
{
	int x;
	int n = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++n] = x;
	}
	cin >> x;
	int d[3] = { 5,3,1 };
	int k = 0;
	int tmp;
	while (k < x) {
		for (int i = d[k] + 1; i <= n; i++) {
			int j = i - d[k];
			tmp = a[i];
			while (j > 0 && tmp < a[j]) {
				a[j + d[k]] = a[j];
				j = j - d[k];
			}
			a[j + d[k]] = tmp;
		}
		k++;
	}
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

四.简单选择排序

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int main()
{
	int x;
	int n = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++n] = x;
	}
	cin >> x;
	for (int i = 1; i <= x; i++) {
		int k = i;
		for (int j = i + 1; j <= n; j++) {
			if (a[j] < a[k]) {
				k = j;
			}
		}
		int tmp = a[i];
		a[i] = a[k];
		a[k] = tmp;
	}
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

五.快速排序

直接sort更简洁,但是要求使用快排

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int part(int l, int r)
{
	a[0] = a[l];
	int p = a[l];
	while (l < r) {
		while (l < r && a[r] >= p) r--;
		a[l] = a[r];
		while (l < r && a[l] <= p) l++;
		a[r] = a[l];
	}
	a[l] = a[0];
	return l;
}
void quick(int l, int r)
{
	if (l < r) {
		int p = part(l, r);
		quick(l, p - 1);
		quick(p + 1, r);
	}
}
int main()
{
	int x;
	int n = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++n] = x;
	}
	quick(1, n);
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

六.堆排序

这是一个大根堆,题目没写

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int a[N];
void part(int k, int n)
{
	int i = k, j;
	int x;
	x = a[i];
	j = 2 * i;
	while (j <= n) {
		if (j < n && a[j] < a[j + 1]) {
			j++;
		}
		if (x < a[j]) {
			a[i] = a[j];
			i = j;
			j = j * 2;
		}
		else {
			j = n + 1;
		}
	}
	a[i] = x;
}
void so(int n)
{
	for (int i = n / 2; i >= 1; i--) {
		part(i, n);
	}
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	for (int i = n; i >= 2; i--) {
		int x = a[1];
		a[1] = a[i];
		a[i] = x;
		part(1, i - 1);
	}
}
int main()
{
	int x;
	int n = 0;
	while (cin >> x) {
		if (x == 0) break;
		a[++n] = x;
	}
	so(n);
	for (int i = 1; i <= n; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值