插入排序 快排 计数(桶排序) 堆排序

//insert sort
由小向大排序

void sort(int input[], int length){
	// implement your code

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

		if (input[i] < input[i - 1]){//找到数 它比它的前一个小
			//找到相应的位置
			for (int j = i; j > 0; --j){//从该位置向前遍历,只到放到合适的位置
				if (input[j] < input[j - 1]){
					int temp = input[j];
					input[j] = input[j - 1];
					input[j - 1] = temp;
				}else{
					break;
				}

			}
		}

	}

//quick sort ======================================================

void quickSort(int num[], int l, int r) {
	if (l >= r)     //先检查左右条件
		return;
	int i = l, j = r, x = num[l];
	while (i < j) {
		while (i < j && num[j] >= x)//从右向左找到第一个小于x的
			j--;
		if (i < j)
			num[i++] = num[j];//填坑之后i++
	
		while (i < j && num[i] <= x)//从左向右找第一个大于x的数
			i++;
		if (i < j)
			num[j--] = num[i];
	}
	num[i] = x;     //把最开始取出来的x放到i处
	quickSort(num, l, i - 1);//以i为中间值,分左右两部分递归调用
	quickSort(num, i + 1, r);
}

把一组数字,一层一层的分两边,左边小右边大。
从右找到第一个小于基础数的数
从左找到第一个大于基准数的数
交换两个数
递归进行

void kp_sort(int A[], int x, int y){
	//递归到最后时,进行特殊处理
	if ((y - x) == 1){
		if (A[y] < A[x]){
			int temp = A[y]; A[y] = A[x]; A[x] = temp;
		}
		return;
	}

	int key = x, left = x, right = y;
	//找到第一个元素的基准位置
	while (right > left){
		//找到右边第一个比key位置上元素小的元素, 且不能小于left
		while (A[right] >= A[key] && right > left){
			--right;
		}
		//找到左边第一个比key位置上元素打的元素, 且不能大于right
		while (A[left] <= A[key] && right > left){
			++left;
		}
		//表示已经找了基准位置
		if (right == left)
			break;

		int temp = A[right];
		A[right] = A[left];
		A[left] = temp;
	}

	//exchange the key and find data
	if (key != right){
		int temp = A[key];
		A[key] = A[right];
		A[right] = temp;
	}
	//再次防止递归越界
	if (right - 1 > x)
		kp_sort(A, x, right - 1);
	if (right + 1 < y)
		kp_sort(A, right + 1, y);
}

void sort(int input[], int length){

	sort_help(input, 0, length-1);
	//kp_sort(input, 0, length - 1);
	//quickSort(input, 0, length-1);
}

//count sort =====================================================

用一个很大的数组,去记录已有的数组,然后依次取出,简单除暴高效,就是占内存

void sort(int input[], int length, int max){
	//printf("Len [%d] Max [%d] \n", length, max);

	for (register int i = 0; i < length; ++i){
		++count[input[i]];
	}

	//register int j = 0;

	for (register int i = 0, j = 0; i < max; ++i){
		for (int k = count[i]; k > 0; --k){
			input[j++] = i;
		}
	
		count[i] = 0;
	}
}

//heap sort=====================================================
堆排序
首先就是建立一个堆,从数组的一半的位置上开始计算,就相当于一个树的叶子节点开始向上建堆,建立的过程依据,上面的元素大于下面的元素进行dfs不断的调整
其次,在这个堆上开始进行dfs,每一次拿出来顶上最大的元素,然后再进行dfs找到现在的堆顶元素,且刚才拿去的元素不再进行dfs

#include <stdio.h>

#define MAX_SIZE 100000

int get_left(int loc){
	return loc * 2 + 1;
}

int get_right(int loc){
	return loc * 2 + 2;
}

void find(int input[MAX_SIZE], int n, int limt);
/*
this is the create tree function
*/
void heap_sort_create(int input[MAX_SIZE], int length){
	int pos = length / 2  - 1;
	
	while (pos > -1)
	{
		int left = get_left(pos); 	int right = get_right(pos);
		int min = pos;
		
		if (left < length && input[left] < input[min]) min = left;
		if (right < length && input[right] < input[min]) min = right;

		if (min != pos) {
			int temp = input[min];  input[min] = input[pos]; input[pos] = temp;
			find(input, min, length);
		}
		--pos;
	}
	
	//exchange the max number
	int temp = input[0]; input[0] = input[length - 1]; input[length - 1] = temp;
}

/*
this is a dfs function, depends on the tree
*/
void find(int input[MAX_SIZE], int n, int limt){
	int loc = n; int min = loc;
	int left = get_left(loc); 	int right = get_right(loc);

	if (left < limt && input[left] < input[loc]) min = left;
	if (right < limt && input[right] < input[min]) min = right;

	if (min != loc){
		int temp = input[min];  input[min] = input[loc]; input[loc] = temp;

		find(input, min, limt);
	}
}

void sort(int input[MAX_SIZE], int length) {

	heap_sort_create(input, length);

	for (int i = length - 1; i >= 2; --i){ //find the min munber in every time
		find(input, 0, i); //dfs to modify this tree
		
		int temp = input[0]; input[0] = input[i - 1]; input[i - 1] = temp;

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值