数据结构——排序算法实现:直接插入排序、折半查找排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序、归并排序、基数排序

1、插入排序

#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 105
#define MAXN 55
typedef int KeyType;
typedef int InfoType;
typedef char BaseType;

typedef struct {
    
    
	KeyType key;
	BaseType Based[MAXN];
	InfoType other;
}RedType;

typedef struct {
    
    
	RedType r[MAXSIZE + 1];
	int length;
	int Basiclength;
}SqList;

int step;

void input(SqList& L) {
    
    
	for (int i = 1; i <= L.length; i++)
		cin >> L.r[i].key;
}

void output(SqList L) {
    
    
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = 1; i <= L.length; ++i)
		cout << setw(5) << L.r[i].key;
	cout << endl;
}

//- - - - -算法8.1 直接插入排序- - - - -
void InsertSort(SqList& L) {
    
    
	int i, j; step = 0;
	for (i = 2; i <= L.length; ++i) {
    
    
		if (L.r[i].key < L.r[i - 1].key) {
    
    
			L.r[0] = L.r[i];
			L.r[i] = L.r[i - 1];
			for (j = i - 2; L.r[0].key < L.r[j].key; --j)
				L.r[j + 1] = L.r[j];
			L.r[j + 1] = L.r[0];
		}
		output(L);
	}
}

//- - - - -算法8.2 折半插入排序- - - - -
void BinaryInsertSort(SqList& L) {
    
    
	int i, j, high, low, mid; step = 0;
	for (i = 2; i <= L.length; ++i) {
    
    
		L.r[0] = L.r[i];
		low = 1;
		high = i - 1;
		while (low <= high) {
    
    
			mid = (low + high) / 2;
			if (L.r[0].key < L.r[mid].key)
				high = mid - 1;
			else
				low = mid + 1;
		}
		for (j = i - 1; j >= high + 1; --j)
			L.r[j + 1] = L.r[j];
		L.r[high + 1] = L.r[0];
		output(L);
	}
}

//- - - - -算法8.3 希尔排序- - - - -
void ShellSort_base(SqList& L, int dk) {
    
    
	int i, j;
	for (i = dk + 1; i <= L.length; ++i) {
    
    
		if (L.r[i].key < L.r[i - dk].key) {
    
    
			L.r[0] = L.r[i];
			for (j = i - dk; j > 0 && L.r[0].key < L.r[j].key; j -= dk)
				L.r[j + dk] = L.r[j];
			L.r[j + dk] = L.r[0];
		}
	}
}

void ShellSort(SqList& L, int dt[], int t) {
    
    
	dt[0] = 5; dt[1] = 3; dt[2] = 1;
	t = 3; step = 0;
	for (int i = 0; i < t; ++i) {
    
    
		ShellSort_base(L, dt[i]);
		output(L);
	}
}

void main() {
    
    
	int n, dt[3];
	SqList L, r;
	memset(&L, 0, sizeof(L));
	memset(&r, 0, sizeof(r));
	cout << "请输入表长:";
	cin >> L.length;
	cout << "请输入最大数字的位数:";
	cin >> L.Basiclength;
	cout << "请输入表中元素:";
	input(L);
	cout << "\n直接插入排序过程演示" << endl;
	r = L;
	InsertSort(r);
	cout << "\n\n折半插入排序过程演示" << endl;
	r = L;
	BinaryInsertSort(r);
	cout << "\n\n希尔排序过程演示" << endl;
	r = L;
	ShellSort(r, dt, 3);
}

测试样例

2、交换排序

#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 105
#define MAXN 55
typedef int KeyType;
typedef int InfoType;
typedef char BaseType;

typedef struct {
    
    
	KeyType key;
	BaseType Based[MAXN];
	InfoType other;
}RedType;

typedef struct {
    
    
	RedType r[MAXSIZE + 1];
	int length;
	int Basiclength;
}SqList;

int step;

void input(SqList& L) {
    
    
	for (int i = 1; i <= L.length; i++)
		cin >> L.r[i].key;
}

void output(SqList L) {
    
    
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = 1; i <= L.length; ++i)
		cout << setw(5) << L.r[i].key;
	cout << endl;
}

//- - - - -算法8.4 冒泡排序- - - - -
void BubbleSort(SqList& L) {
    
    
	int i, j; step = 0;
	RedType t;
	for (i = L.length; i > 0; --i) {
    
    
		for (j = 1; j < i; ++j) {
    
    
			if (L.r[j].key > L.r[j + 1].key) {
    
    
				t = L.r[j];
				L.r[j] = L.r[j + 1];
				L.r[j + 1] = t;
			}
		}
		output(L);
	}
}

//- - - - -算法8.5 快速排序- - - - -
int QuickSort_base(SqList& L, int low, int high) {
    
    
	L.r[0] = L.r[low];
	int key = L.r[low].key;
	while (low < high) {
    
    
		while (low < high && L.r[high].key >= key)
			--high;
		L.r[low] = L.r[high];
		while (low < high && L.r[low].key <= key)
			++low;
		L.r[high] = L.r[low];
	}
	L.r[low] = L.r[0];
	return low;
}

void QuickSort(SqList& L, int low, int high) {
    
    
	if (low < high) {
    
    
		int loc = QuickSort_base(L, low, high);
		QuickSort(L, low, loc - 1);
		QuickSort(L, loc + 1, high);
		output(L);
	}
}

void main() {
    
    
	SqList L, r;
	memset(&L, 0, sizeof(L));
	memset(&r, 0, sizeof(r));
	cout << "请输入表长:";
	cin >> L.length;
	cout << "请输入最大数字的位数:";
	cin >> L.Basiclength;
	cout << "请输入表中元素:";
	input(L);
	cout << "\n冒泡排序过程演示" << endl;
	r = L;
	BubbleSort(r);
	cout << "\n\n快速排序过程演示" << endl;
	step = 0;
	r = L;
	QuickSort(r, 1, r.length);
}

测试样例

3、选择排序

#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 105
#define MAXN 55
typedef int KeyType;
typedef int InfoType;
typedef char BaseType;

typedef struct {
    
    
	KeyType key;
	BaseType Based[MAXN];
	InfoType other;
}RedType;

typedef struct {
    
    
	RedType r[MAXSIZE + 1];
	int length;
	int Basiclength;
}SqList;

int step;

void input(SqList& L) {
    
    
	for (int i = 1; i <= L.length; i++)
		cin >> L.r[i].key;
}

void output(SqList L) {
    
    
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = 1; i <= L.length; ++i)
		cout << setw(5) << L.r[i].key;
	cout << endl;
}

//- - - - -算法8.6 简单选择排序- - - - -
void SelectSort(SqList& L) {
    
    
	int i, j, k; step = 0;
	for (i = 1; i < L.length; ++i) {
    
    
		k = i;
		for (j = i + 1; j <= L.length; ++j)
			if (L.r[j].key < L.r[k].key)
				k = j;
		if (k != i) {
    
    
			L.r[0] = L.r[i];
			L.r[i] = L.r[k];
			L.r[k] = L.r[0];
		}
		output(L);
	}
}

//- - - - -算法8.7 筛选法调整堆- - - - -
void HeapSort_adjust(SqList& L, int s, int m) {
    
    
	RedType r = L.r[s];
	int j;
	for (j = 2 * s; j <= m; j *= 2) {
    
    
		if (j < m && L.r[j].key < L.r[j + 1].key)
			++j;
		if (r.key >= L.r[j].key)
			break;
		L.r[s] = L.r[j];
		s = j;
	}
	L.r[s] = r;
}

//- - - - -算法8.8 建初堆- - - - -
void HeapSort_base(SqList& L) {
    
    
	int n = L.length;
	for (int i = n / 2; i > 0; --i)
		HeapSort_adjust(L, i, n);
}

//- - - - -算法8.9 堆排序- - - - -
void HeapSort(SqList& L) {
    
    
	step = 0;
	HeapSort_base(L);
	RedType x;
	for (int i = L.length; i > 1; --i) {
    
    
		x = L.r[1];
		L.r[1] = L.r[i];
		L.r[i] = x;
		HeapSort_adjust(L, 1, i - 1);
		output(L);
	}
}

void main() {
    
    
	SqList L, r;
	memset(&L, 0, sizeof(L));
	memset(&r, 0, sizeof(r));
	cout << "请输入表长:";
	cin >> L.length;
	cout << "请输入最大数字的位数:";
	cin >> L.Basiclength;
	cout << "请输入表中元素:";
	input(L);
	cout << "\n简单选择排序过程演示" << endl;
	r = L;
	SelectSort(r);
	cout << "\n\n堆排序过程演示" << endl;
	r = L;
	HeapSort(r);
}

测试样例

4、归并排序

#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 105
#define MAXN 55
typedef int KeyType;
typedef int InfoType;
typedef char BaseType;

typedef struct {
    
    
	KeyType key;
	BaseType Based[MAXN];
	InfoType other;
}RedType;

typedef struct {
    
    
	RedType r[MAXSIZE + 1];
	int length;
	int Basiclength;
}SqList;

int step;

void input(SqList& L) {
    
    
	for (int i = 1; i <= L.length; i++)
		cin >> L.r[i].key;
}

void output(SqList L) {
    
    
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = 1; i <= L.length; ++i)
		cout << setw(5) << L.r[i].key;
	cout << endl;
}

//- - - - -算法8.10 相邻两个有序子序列的归并- - - - -
void MergeSort_base1(RedType T[], int low, int mid, int high) {
    
    
	RedType R[MAXSIZE];
	memset(R, 0, sizeof(R));
	int i = low, j = mid + 1, k = low;
	while (i <= mid && j <= high) {
    
    
		if (T[i].key <= T[j].key)R[k++] = T[i++];
		else R[k++] = T[j++];
	}
	while (i <= mid)R[k++] = T[i++];
	while (j <= high)R[k++] = T[j++];
	for (int i = low; i <= high; i++)
		T[i] = R[i];
}

void MergeSort_base2(RedType T[], int low, int high) {
    
    
	if (low < high) {
    
    
		int mid = (low + high) / 2;
		MergeSort_base2(T, low, mid);
		MergeSort_base2(T, mid + 1, high);
		MergeSort_base1(T, low, mid, high);
	}
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = low; i <= high; i++) {
    
    
		cout << setw(5) << T[i].key;
	}
	cout << endl;
}

//- - - - -算法8.11 归并排序- - - - -
void MergeSort(SqList& L) {
    
    
	step = 0;
	MergeSort_base2(L.r, 1, L.length);
	output(L);
}

void main() {
    
    
	SqList L, r;
	memset(&L, 0, sizeof(L));
	memset(&r, 0, sizeof(r));
	cout << "请输入表长:";
	cin >> L.length;
	cout << "请输入最大数字的位数:";
	cin >> L.Basiclength;
	cout << "请输入表中元素:";
	input(L);
	cout << "\n归并排序过程演示" << endl;
	r = L;
	MergeSort(r);
}

测试样例

5、基数排序

#include<iostream>
#include<iomanip>
using namespace std;

#define MAXSIZE 105
#define MAXN 55
typedef int KeyType;
typedef int InfoType;
typedef char BaseType;

typedef struct {
    
    
	KeyType key;
	BaseType Based[MAXN];
	InfoType other;
}RedType;

typedef struct {
    
    
	RedType r[MAXSIZE + 1];
	int length;
	int Basiclength;
}SqList;

int step;

void input(SqList& L) {
    
    
	for (int i = 1; i <= L.length; i++)
		cin >> L.r[i].key;
}

void output(SqList L) {
    
    
	cout << "step" << setw(2) << ++step << "\t线性表为:";
	for (int i = 1; i <= L.length; ++i)
		cout << setw(5) << L.r[i].key;
	cout << endl;
}

//- - - - -算法8.12 基数排序- - - - -
void TurnInput(SqList& L) {
    
    
	//默认关键字为数值,转换成字符串,最低基位在前,从字符串前比较
	for (int i = 1; i <= L.length; i++) {
    
    
		int k = 0;
		for (int j = L.r[i].key; j > 0; j /= 10) {
    
    
			int r = j % 10;
			L.r[i].Based[k++] = r;
		}
	}
}

RedType Box[10][MAXN];

void RadixSort_base(SqList& L, int Baseplace) {
    
    
	memset(Box, 0, sizeof(Box));
	int i, box[10] = {
    
     0 }, box1, box2;
	for (i = 1; i <= L.length; i++) {
    
    
		int num = L.r[i].Based[Baseplace];//基位数数值
		Box[num][box[num]] = L.r[i];
		Box[num][box[num]].other = 1;
		box[num]++;
	}
	box1 = 0, box2 = 0;
	for (i = 1; i <= L.length; i++) {
    
    
		while (box1 < 10 && box2 < 10 && Box[box1][box2].other == 0) {
    
    
			box1++;
			box2 = 0;
		}
		L.r[i] = Box[box1][box2++];
	}
}

void RadixSort(SqList& L) {
    
    
	step = 0;
	for (int i = 0; i < L.Basiclength; ++i) {
    
    
		RadixSort_base(L, i);
		output(L);
	}
}

void main() {
    
    
	SqList L;
	memset(&L, 0, sizeof(L));
	cout << "请输入表长:";
	cin >> L.length;
	cout << "请输入最大数字的位数:";
	cin >> L.Basiclength;
	cout << "请输入表中元素:";
	input(L);
	cout << "\n基数排序过程演示" << endl;
	TurnInput(L);
	RadixSort(L);
}

测试样例怼着别人的代码抄的,有待完善!

猜你喜欢

转载自blog.csdn.net/cwindyc/article/details/106696504