排序

 

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

struct TreeNode {
	int value;
	TreeNode* left;
	TreeNode* right;
};
//快排
void quicksort(int* que, const int length) {
	//长度为1的数组或数组为空指针
	if (length == 1 || que == nullptr)return;

	int mid = que[0];
	int i_pointer = 0;
	int j_pointer = length - 1;
	while (i_pointer < j_pointer) {
		//i和j相等或者找到比mid小的数停止左移
		while (i_pointer < j_pointer&&que[j_pointer] >= mid) {
			j_pointer--;
		}
		//i和j相等或者找到比mid大的数停止右移
		while (i_pointer < j_pointer&&que[i_pointer] <= mid) {
			i_pointer++;
		}
		//交换i和j指向的值
		if (i_pointer != j_pointer) {
			int temp = que[i_pointer];
			que[i_pointer] = que[j_pointer];
			que[j_pointer] = temp;
		}
	}
	//把基数放到中间
	int temp = que[i_pointer];
	que[i_pointer] = que[0];
	que[0] = temp;
	//左右两边递归调用该函数
	if (i_pointer > 1) {
		quicksort(&que[0], i_pointer);
	}
	if (j_pointer < length - 2) {
		quicksort(&que[j_pointer + 1], length - j_pointer - 1);
	}
}
//堆排
void adjust(int* que, const int length, int index) {
	int left = index * 2 + 1;
	int right = index * 2 + 2;
	int maxindex = index;
	if (left < length) {
		if (que[left] > que[maxindex])maxindex = left;
		if (right<length&&que[right] > que[maxindex])maxindex = right;
		if (maxindex != index) {
			swap(que[index], que[maxindex]);
			adjust(que, length, maxindex);
		}
	}
}
void build_heap(int* que, const int length) {
	if (length == 1 || que == nullptr)return;
	int first_non_leaf = (length - 2) / 2;
	//建立大根堆
	//1.从下到上,从右到左调整每个非叶节点
	//2.比较非叶节点和他的孩子,父亲节点取到三者最大
	//3.如果父亲节点和子节点交换,递归的调整子树为大根堆
	for (int i = first_non_leaf; i >= 0; i--) {
		adjust(que, length, i);
	}
	//堆排序
	for (int i = 0; i < length; i++) {
		//1.收尾交换
		swap(que[0], que[length - 1 - i]);
		//2.调整剩余元素为大根堆
		adjust(que, length - i - 1, 0);
	}

}
//归并
void merge(int* que, int* result, int left_index, int right_index, const int length) {
	int mid = (length - 1) / 2;
	int index = 0;
	for (int i = 0; i < length; i++) {
		if (left_index == (mid+1)&&right_index<length) {
			result[index++] = que[right_index++];
		}
		else if (right_index == length&&left_index<(mid + 1)) {
			result[index++] = que[left_index++];
		}
		else {
			if (que[left_index] <= que[right_index]) {
				result[index++] = que[left_index++];
			}
			else {
				result[index++] = que[right_index++];
			}
		}
	}
}
void merge_sort(int* que, const int length, int* result) {
	if (length == 0 || que == nullptr)throw exception("invalid input");
	if (length == 1)return;
	if (length == 2) {
		int temp = 0;
		if (que[0] > que[1]) {
			temp = que[0];
			que[0] = que[1];
			que[1] = temp;
		}
	}
	else {
		int mid = (length - 1) / 2;
		merge_sort(que, mid + 1, result);
		merge_sort(&que[mid + 1], length - mid - 1, result);
		merge(que, result, 0, mid + 1, length);
		for (int i = 0; i < length; i++) {
			que[i] = result[i];
		}
	}
}



void test1() {
	const int length = 12;
	int que[length] = { 10,14,2,1,11,5,3,8,18,20,6,4 };
	//int* que = nullptr;
	quicksort(que, length);
	for (int i = 0; i < length; i++) {
		cout << que[i] << " ";
	}
	cout << endl;
}
void test2() {
	const int length = 1;
	int que[length] = { 3 };
	build_heap(que, length);
	for (int i = 0; i < length; i++) {
		cout << que[i] << " ";
	}
	cout << endl;
}
void test3() {
	const int length = 8;
	int que[length] = { 2,1,6,5,4,3,10,7 };
	int result[length];
	//int* que = nullptr;
	//int* result = nullptr;
	try {
		merge_sort(que, length, result);
	}
	catch (exception &e) {
		cout << e.what() << endl;
	}
	for (int i = 0; i < length; i++) {
		cout << que[i] << " ";
	}
	cout << endl;
}
int main() {
	//test1();//快排
	//test2();//堆排序
	//test3();//归并排序
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值