八大排序——希尔、归并、快速排序

希尔排序

//希尔排序

void insertSort(int *arr, int len,int level)
{
	assert(NULL != arr);
	static int b = 0;

	for (int i = level; i < len; i+=level)
	{
		int j = i;
		int tmp = arr[i];
		for (; j > 0; j-=level)
		{
			b++;
			if (tmp > arr[j - level])
			{
				arr[j] = arr[j - level];	
			}
			else
			{
				break;
			}
		}
		arr[j] = tmp;
	}
	printf("%d\n", b);
}
void shellSort(int *arr, int len)//时间复杂度O(n^1.5-1.3)  空间复杂度O(1)  不稳定
{
	assert(NULL != arr);

	int level[] = {7,5, 3, 1 };
	for (int i = 0; i < 4; i++)
	{
		insertSort(arr, len, level[i]);
	}
}

归并排序

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

void mergeOnly(int *arr, int len, int grap)
{
	assert(NULL != arr);
	int *tmp = (int*)malloc(len*sizeof(int));
	assert(NULL != tmp);
	int i = 0;

	int low1 = 0;
	int hight1 = low1 + grap - 1;
	int low2 = hight1+ 1;
	int hight2 = low2 + grap - 1 >= len - 1 ? len - 1 : low2 + grap - 1;

	while (low2 < len)
	{
		while (low1 <= hight1 && low2 <= hight2)
		{
			if (arr[low1] <= arr[low2])
			{
				tmp[i++] = arr[low1++];
			}
			else
			{
				tmp[i++] = arr[low2++];
			}
		}

		while (low1 <= hight1)
		{
			tmp[i++] = arr[low1++];
		}
		while (low2 <= hight2)
		{
			tmp[i++] = arr[low2++];
		}

		low1 = hight2 + 1;
		hight1 = low1 + grap - 1;
		low2 = hight1 + 1;
		hight2 = low2 + grap - 1 >= len - 1 ? len - 1 : low2 + grap - 1;
	}

	while (low1 < len)
	{
		tmp[i++] = arr[low1++];
	}

	for (int j = 0; j < len; j++)
	{
		arr[j] = tmp[j];
	}

}


void mergeSort(int *arr, int len)
{
	assert(NULL != arr);
	for (int i = 1; i < len; i *= 2)
	{
		mergeOnly(arr, len, i);
	}
}

int main()
{
	int arr[] = { 23, 56, 78, 43, 12, 98, 65, 34, 87, 10 };
	mergeSort(arr, sizeof(arr) / sizeof(arr[0]));

	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%d  ", arr[i]);
	}
	printf("\n");

	return 0;
}

快速排序 

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stack>
#include<time.h>
using namespace std;

int partition(int *arr, int low, int hight)
{
	assert(NULL != arr);
	int tmp = arr[low];

	while (low < hight)//O(n)
	{
		while (low < hight && arr[hight] >= tmp)
		{
			hight--;
		}
		if (low < hight)
		{
			arr[low++] = arr[hight];
		}
		else
		{
			break;
		}

		while (low < hight && arr[low] <= tmp)
		{
			low++;
		}
		if (low < hight)
		{
			arr[hight--] = arr[low];
		}
		else
		{
			break;
		}
	}
	arr[low] = tmp;

	return low;
}


void quickStack(int *arr,int low,int hight)
{
	assert(NULL != arr);/*#include<stack>using namespace std;*/
	stack<int> sta;
	sta.push(low);
	sta.push(hight);

	while (!sta.empty())
	{
		hight = sta.top();
		sta.pop();
		low = sta.top();
		sta.pop();

		int sit = partition(arr, low, hight);

		if (sit > low + 1)
		{
			sta.push(low);
			sta.push(sit - 1);
		}
		if (sit < hight - 1)
		{
			sta.push(sit + 1);
			sta.push(hight);
		}
	}


}

//快排
void quicklySort(int *arr, int len)
{
	quickStack(arr, 0, len - 1);
}

int main()
{
	srand(time(0));

	int arr[10] = {0};
	for (int i = 0; i < 10; i++)
	{
		arr[i] = rand() % 100 + 1;
	}

	quicklySort(arr, 10);
	for (int i = 0; i < 10; i++)
	{
		printf("%d  ", arr[i]);
	}
	printf("\n");

	return 0;
}

快速排序:越有序越慢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值