深圳大学计软《数据结构》实验15--内部排序2

问题 A: DS排序–希尔排序

题目描述

给出一个数据序列,使用希尔排序算法进行降序排序。

间隔gap使用序列长度循环除2直到1

输入

第一行输入t,表示有t个测试示例
第二行输入n,表示第一个示例有n个数据(n>1)
第三行输入n个数据,都是正整数,数据之间用空格隔开
以此类推

输出

对每组测试数据,输出每趟排序结果。不同组测试数据间用空行分隔。

样例输入

2
6
111 22 6 444 333 55
8
77 555 33 1 444 77 666 2222

样例输出

444 333 55 111 22 6
444 333 111 55 22 6

444 555 666 2222 77 77 33 1
666 2222 444 555 77 77 33 1
2222 666 555 444 77 77 33 1

AC代码

#include<iostream>
using namespace std;

class Problem
{
public:
	Problem();
	~Problem();
	void print()
	{
		for (int i = 1; i < len;i++)
			cout << arr[i] << " ";
		cout << arr[len] << endl;
	}

	void ShellSort();

private:
	int* arr;
	int len;
	void InsertSort(int gap, int m)
	{
		for (int i = gap + m; i <= len; i += gap)
		{
			int temp = arr[i];
			int j;
			for (j = i - gap; j > 0; j -= gap)
			{
				if (temp <= arr[j])
					break;
				arr[j + gap] = arr[j];
			}
			arr[j + gap] = temp;
		}
	}
};

Problem::Problem()
{
	cin >> len;
	arr = new int[len + 1];
	for (int i = 1; i <= len; i++)
		cin >> arr[i];
}


Problem::~Problem()
{
	delete[]arr;
}

void Problem::ShellSort()
{
	for (int gap = len / 2; gap >= 1; gap /= 2) 
	{
		for (int m = 1; m <= gap; m++)
			InsertSort(gap, m);
		print();
	}

}


int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		if (i)cout << endl;
		Problem p;
		p.ShellSort();
	}
	return 0;
}

问题 B: 冒泡排序 (Ver. I)

题目描述

给定一个包含从0到n-1各一次的数组,若使用冒泡排序将其排为升序,问其中需要进行多少次交换

输入

测试数据有多组,

每组由两行组成:第一行包含正整数n(n <= 5000); 下一行包含从0到n-1的n个整数的序列。

输出

对于每组测试数据,

输出交换次数

样例输入

10
1 3 6 9 0 8 5 7 4 2

样例输出

22

AC代码

#include<iostream>
using namespace std;

void swap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}


int BubbleSort(int* p, int num) {
	int cnt = 0;
	for (int i = 1; i < num; i++) {
		bool flag = 0;
		for (int j = 0; j < num - i; j++)
			if (p[j] > p[j + 1]) {
				swap(p[j], p[j + 1]);
				flag = 1;
				cnt++;
			}
		if (!flag)
			break;
	}
	return cnt;
}

int main() {
	int n;
	while (cin >> n) {
		int* arr = new int[n];
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		cout << BubbleSort(arr, n) << endl;
		delete[]arr;
	}
	return 0;
}

问题 C: DS排序–快速排序

题目描述

给出一个数据序列,使用快速排序算法进行从小到大的排序

–程序要求–
若使用C++只能include一个头文件iostream;若使用C语言只能include一个头文件stdio
程序中若include多过一个头文件,不看代码,作0分处理
不允许使用第三方对象或函数实现本题的要求

输入

第一行输入t,表示有t个测试示例
第二行输入n,表示第一个示例有n个数据
第三行输入n个数据,都是正整数,数据之间用空格隔开
以此类推

输出

每组测试数据,输出每趟快排的结果,即每次排好一个数字结果(长度为1的子序列,不用排,不用输出)。不同测试数据间用空行分隔。

样例输入

2
6
111 22 6 444 333 55
8
77 555 33 1 444 77 666 2222

样例输出

55 22 6 111 333 444
6 22 55 111 333 444
6 22 55 111 333 444
6 22 55 111 333 444

1 33 77 555 444 77 666 2222
1 33 77 555 444 77 666 2222
1 33 77 77 444 555 666 2222
1 33 77 77 444 555 666 2222
1 33 77 77 444 555 666 2222

AC代码

#include<iostream>
using namespace std;

class Problem
{
public:
	Problem();
	~Problem();
	void print()
	{
		for (int i = 1; i < len; i++)
			cout << arr[i] << " ";
		cout << arr[len] << endl;
	}
	void QSort();

private:
	int* arr;
	int len;
	void QSort(int, int);
};

Problem::Problem()
{
	cin >> len;
	arr = new int[len + 1];
	for (int i = 1; i <= len; i++)
		cin >> arr[i];
}


Problem::~Problem()
{
	delete[]arr;
}

void Problem::QSort(int low, int high)
{
	int i = low, j = high, p = arr[low];
	while (low < high)
	{
		while (low < high && arr[high] >= p)
			high--;
		if (low >= high)
		{
			//print();
			break;
		}
		arr[low] = arr[high];
		low++;
		while (low < high && arr[low] <= p)
			low++;
		if (low < high)
		{
			arr[high] = arr[low];
			high--;
		}
	}
		arr[low] = p;
		print();

		if (i < low - 1)
			QSort(i, low - 1);
		if (high + 1 < j)
			QSort(high + 1, j);
	
}

void Problem::QSort() { QSort(1, len); }

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		if (i)cout << endl;
		Problem p;
		p.QSort();
		//p.print();
	}
	return 0;
}

问题 D: DS内排—堆排序

题目描述

给定一组数据,使用堆排序完成数据的降序排序。(建小顶堆)。

输入

数据个数n,n个整数数据

输出

初始创建的小顶堆序列

每趟交换、筛选后的数据序列,输出格式见样例

样例输入

8 34 23 677 2 1 453 3 7

样例输出

8 1 2 3 7 23 453 677 34
8 2 7 3 34 23 453 677 1
8 3 7 453 34 23 677 2 1
8 7 23 453 34 677 3 2 1
8 23 34 453 677 7 3 2 1
8 34 677 453 23 7 3 2 1
8 453 677 34 23 7 3 2 1
8 677 453 34 23 7 3 2 1

AC代码

#include <iostream>
using namespace std;


class Problem{
	int* arr;
	int num;
public:
	Problem() { cin >> num; arr = new int[num]; for (int i = 0; i < num; i++)cin >> arr[i]; }
	~Problem() { delete[]arr; }
	void print_array()
	{
		cout << num;
		for (int i = 0; i < num; i++)
			cout << " " << arr[i];
		cout << endl;
	}

	void min_heapify(int start, int end)
	{
		int parent = start;
		int son = parent * 2 + 1;
		while (son <= end)
		{
			if (son + 1 <= end && arr[son] > arr[son + 1])
				son++;
			if (arr[parent] < arr[son])
				return;
			swap(arr[parent], arr[son]);
			parent = son;
			son = parent * 2 + 1;
		}
	}

	void heap_sort()
	{
		for (int i = num / 2 - 1; i >= 0; i--)
			min_heapify(i, num - 1);
		print_array();
		for (int i = num - 1; i > 0; i--)
		{
			swap(arr[0], arr[i]);
			min_heapify(0, i - 1);
			print_array();
		}
	}

};


int main()
{
	Problem p;
	p.heap_sort();
	return 0;
}

问题 E: DS顺序表–类实现

题目描述

实现
顺序表的用C++语言和类实现顺序表

属性包括:数组、实际长度、最大长度(设定为1000)

操作包括:创建、插入、删除、查找

类定义参考
在这里插入图片描述

输入

第1行先输入n表示有n个数据,即n是实际长度;接着输入n个数据
第2行输入要插入的位置和新数据
第3行输入要插入的位置和新数据
第4行输入要删除的位置
第5行输入要删除的位置
第6行输入要查找的位置
第7行输入要查找的位置

输出

数据之间用空格隔开

第1行输出创建后的顺序表内容,包括顺序表实际长度和数据

每成功执行一次操作(插入或删除),输出执行后的顺序表内容

每成功执行一次查找,输出查找到的数据

如果执行操作失败(包括插入、删除、查找等失败),输出字符串error,不必输出顺序表内容

样例输入

6 11 22 33 44 55 66
3 777
1 888
1
9
0
5

样例输出

6 11 22 33 44 55 66
7 11 22 777 33 44 55 66
8 888 11 22 777 33 44 55 66
7 11 22 777 33 44 55 66
error
error
44

提示

第i个位置是逻辑意义的位置,从1开始,在实际编程用数组,从0开始,对应数组i-1位置

AC代码

#include<iostream>
using namespace std;

class SeqList
{
public:
	SeqList();
	~SeqList();
	void print();
	void insert(int index, int data);
	void erase(int index);
	void get(int index);

private:
	int maxsize;
	int size;
	int* list;
};

SeqList::SeqList()
{
	maxsize = 1000;
	cin >> size;
	list = new int[maxsize];
	for (int i = 0; i < size; i++)
		cin >> list[i];
	print();
}

SeqList::~SeqList()
{
	delete[]list;
}

void SeqList::print()
{
	cout << size;
	for (int i = 0; i < size; i++)
		printf(" %d", list[i]);
	putchar('\n');
}


void SeqList::insert(int index, int data)
{
	if (index<0 || index>size)
	{
		puts("error");
		return;
	}
	for (int i = size; i > index; i--)
		list[i] = list[i - 1];
	list[index] = data;
	size++;
	print();
}

void SeqList::erase(int index)
{
	if (index < 0 || index >= size)
	{
		puts("error");
		return;
	}
	for (int i = index; i < size - 1; i++)
		list[i] = list[i + 1];
	size--;
	print();
}



void SeqList::get(int index)
{
	if (index < 0 || index >= size)
	{
		puts("error");
		return;
	}
	cout << list[index] << endl;
}

int main() {
	SeqList l;
	int index, data;
	cin >> index >> data;
	l.insert(index - 1, data);
	cin >> index >> data;
	l.insert(index - 1, data);
	cin >> index;
	l.erase(index - 1);
	cin >> index;
	l.erase(index - 1);
	cin >> index;
	l.get(index - 1);
	cin >> index;
	l.get(index - 1);
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹无悔

请支持我的梦想!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值