数据结构排序方法总结(C语言)

数据结构排序方法总结(C语言)

#include <iostream>
using namespace std;

#define MAXSIZE 100
typedef int KeyType; //关键字的类型
typedef struct {
	KeyType key; //关键字像
	/*可能有其他数据项,比如一个学生,有name,id,score,则key可能是其中的一个*/
} RcdType;

typedef struct List *SqList; //
struct List { //顺序表
	int length;
	RcdType r[MAXSIZE + 1];
} ;

SqList InitSqList() { //顺序表初始化
	SqList L = (SqList)malloc(sizeof (struct List));
	int length, key;
	cout << "please input the length of the sequence table:" << endl;
	cin >> length; //确定长度
	L->length = length;
	cout << "please give the keys of the table:" << endl;

	for (int i = 1; i <= length; i++) { //输入关键字项

		cin >> key;
		L->r[i].key = key;
	}

	return L;
}

void printSqList(SqList L) { //按照一定格式打印顺序表
	cout << "the sequence table is:" << endl;

	for (int i = 1; i <= L->length; i++) {

		cout << L->r[i].key << "\t";
	}

	cout << endl;
}

/*以下所有排序均为升序排序*/

//1.直接插入排序

void InsertionSort(SqList L) {
	int i, j;

	for (i = 2; i <= L->length; i++) {

		if (L->r[i].key < L->r[i - 1].key) {
			L->r[0] = L->r[i];

			for (j = i - 1; j >= 1 ; j--) {

				L->r[j + 1] = L->r[j];
			}

			L->r[j + 1] = L->r[0];
		}
	}
}

//2.折半插入排序
void BiInsertionSort(SqList L) {
	int low, high, mid = 0;

	for (int i = 2; i <= L->length; i++) {

		low = 0;
		high = i - 1;

		if (L->r[i].key < L->r[i - 1].key) {
			L->r[0] = L->r[i];

			while (low <= high) {
				mid = (low + high) / 2;

				if (L->r[0].key < L->r[mid].key) {
					high = mid - 1;
				} else {
					low = mid + 1;
				}
			}

			for (int j = i - 1; j >= high + 1 ; j-- ) {

				L->r[j + 1] = L->r[j];
			}

			L->r[high + 1] = L->r[0];

		}
	}

}

//3.希尔排序
void ShellSort(SqList L) {
	int i, j;

	for (int increment = L->length / 2; increment >= 1; increment /= 2) {

		for (i = increment; i <= L->length; i++) {

			L->r[0] = L->r[i];

			for (j = i - increment; j >= 1 && L->r[j].key > L->r[0].key ; j -= increment) {

				L->r[j + increment] = L->r[j];
			}

			L->r[j + increment] = L->r[0];
		}
	}
}

//4.快速排序
void QSort(SqList L, int low, int high) {
	int Il, Ih, position; //分别是第一次排序时low,high的位置和每一次枢纽的位置记录
	Il = low, Ih = high;
	KeyType pivotKey;
	L->r[0] = L->r[low];
	pivotKey = L->r[low].key;

	while (low < high) {
		while (low < high && L->r[high].key >= pivotKey)
			high--;
		L->r[low] = L->r[high];

		while (low < high && L->r[low].key <= pivotKey)
			low++;
		L->r[high] = L->r[low];
	}

	L->r[low] = L->r[0];
	position = low;

	if (Il < Ih) {
		QSort(L, Il, position - 1);
		QSort(L, position + 1, Ih);
	}
}

//5.冒泡排序
void BubbleSort(SqList L) {
	for (int i = 1; i <= L->length; i++) {

		for (int j = 1; j < L->length - i; j++) {

			if (L->r[j].key > L->r[j + 1].key) {
				L->r[0] = L->r[j];
				L->r[j] = L->r[j + 1];
				L->r[j + 1] = L->r[0];
			}
		}
	}
}

//6.选择排序
void SelectionSort(SqList L) {
	int k;

	for (int i = 1; i <= L->length; i++) {

		k = i;

		for (int j = i + 1; j <= L->length; j++) {

			if (L->r[j].key < L->r[k].key) {
				k = j;
			}
		}

		L->r[0] = L->r[k];
		L->r[k] = L->r[i];
		L->r[i] = L->r[0];
	}
}

//7.堆排序
//7.1 筛选算法
void HeapAdjust(SqList L, int m, int size) {
	L->r[0] = L->r[m];

	for (int j = 2 * m; j <= size; j *= 2) {

		if (j + 1 <= size && L->r[j + 1].key > L->r[j].key) {
			j++;
		}

		if (L->r[j].key < L->r[0].key) {
			break;
		}

		L->r[m] = L->r[j];
		m = j;
	}

	L->r[m] = L->r[0];
}

//7.2建造堆
void BulidHeap(SqList L, int size) {
	for (int i = size / 2; i >= 1; i--) {

		HeapAdjust(L, i, size);
	}
}

//7.3堆排序
void HeapSort(SqList L, int size) {
	RcdType temp;
	BulidHeap(L, size);

	for (int i = size; i >= 1; i--) {

		temp = L->r[i];
		L->r[i] = L->r[1];
		L->r[1] = temp;
		HeapAdjust(L, 1, i - 1);
	}
}

void chooseSortWay(SqList L) {
	int choice;
	cout << "You can choose the way of SORT as follows:" << endl;
	cout << endl;
	cout << "1.Direct insertion sort" << endl;
	cout << "2.Binary Insertion Sort" << endl;
	cout << "3.Shell Sort" << endl;
	cout << "4.Quick Sort" << endl;
	cout << "5.Bubble Sort" << endl;
	cout << "6.Selection Sort" << endl;
	cout << "7.Heap Sort" << endl;
	cout << endl;
	cout << "choose the way :";
	cin >> choice;

	switch (choice) {
		case 1:
			InsertionSort(L);
			break;

		case 2:
			BiInsertionSort(L);
			break;

		case 3:
			ShellSort(L);
			break;

		case 4:
			QSort(L, 1, L->length);
			break;

		case 5:
			BubbleSort(L);
			break;

		case 6:
			SelectionSort(L);
			break;

		case 7:
			HeapSort(L, L->length);
			break;

		default:
			cout << "this way is not existed" << endl;
			chooseSortWay(L);
	}
}


int main(void) {
	SqList L = InitSqList();
	//chooseSortWay(L);
	BubbleSort(L);
	printSqList(L);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

THE WHY

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值