排序

void printfarray(int array[], int icount)
{
	if (array == NULL || icount == 0)
		return;
	for (int i = 0; i < icount; i++)
	{
		std::cout << array[i]<<"  ";
	}
	std::cout << std::endl;
}

//---------------//默认都是升序--------------
// 选择排序 (从头选择一个 和后面的挨个比 记录小的那个元素位置 或者交换到前面)

void SelectSort(int array[],int icount)
{
	if (array == NULL || icount <= 0)
		return;
	int i = 0, j = 0;
	int  k = 0, tmp = 0;
	for (i = 0; i < icount; i++)
	{
		k = i;//默认小
		for (j = i + 1; j < icount; j++)
		{
			if (array[k]>array[j])
			{
				k = j;
			}
		}
		tmp = array[k];
		array[k] = array[i];
		array[i] = tmp;
	}
}
void SelectSort2(int array[], int icount)
{
	if (array == NULL || icount <= 0)
		return;
	int i = 0, j = 0;
	int  k = 0, tmp = 0;
	for (i = 0; i < icount; i++)
	{
		k = i;//默认小
		for (j = i + 1; j < icount; j++)
		{
			if (array[i] > array[j])
			{
				tmp = array[i];
				array[i] = array[j];
				array[j] = tmp;
			}

		}
		
	}
}

//插入排序 (拿出来 满足条件的往后移动)


void InsertSort(int array[], int icount)
{
	if (array == NULL || icount <= 0)
		return;
	int i = 0, j = 0,k=0;
	int tmp = 0;
	for (i = 1; i < icount; i++)
	{
		k = i;//记录空的位置
		tmp = array[k];//拿出来
		for (int j = i - 1; j > 0; j--)
		{
			if (array[j] > array[k])//前面>后面  大的往后移动
			{
				array[k] = array[j];
				k = j;
			}
		}
		array[k] = tmp;
	}
}

//冒泡排序(相邻两个元素比较 交换 )


void BubbleSort(int array[], int icount)
{
	if (array == NULL || icount <= 0)
		return;
	int itmp = 0;
	bool bIsSorted = false;//数据是否已经有序了 默认乱序
	for (int i = 0; i < icount; i++)
	{
		if (bIsSorted)
			break;
		bIsSorted = true;//假设已经排好序了 如果下面没改变bIsSorted 状态 那就是有序的
		for (int j = icount - 1; j > i; j--)
		{
			if (array[j] < array[j - 1])
			{
				itmp = array[j];
				array[j] = array[j - 1];
				array[j - 1] = itmp;
				bIsSorted = false;
			}
		}
	}
}

//希尔排序(nlogn 不稳定排序) 间隔分组后 插入排序(拿出来 满足条件后移)


void ShellSort(int array[], int icount)
{
	if (array == NULL || icount <= 0)
		return;
	int k = -1,tmp=0;
	int i = 0, j = 0;
	int igap =icount;
	do
	{
		igap = igap / 3 + 1;
		for (i = igap; i < icount; i += igap)
		{
			k = i;//记录空的位置
			tmp = array[k];
			for (j = i - igap; j > 0; j-=igap)
			{
				if (array[j]>array[k])
				{
					array[k] = array[j];
					k = j;
				}

			}
			array[k] = tmp;

		}
	} while (igap > 1);
}

//快排( nlogn 不稳定 1、数据分成两份 2、左右partion(左<右))

int Partition(int array[], int ilow, int ihigh)
{
	if (array == NULL || ilow > ihigh)
		return 0;
	int itmp=0;
	int iposValue = array[ilow];//左边拿出来
	while (ilow < ihigh)
	{
		//右边移动
		while ( (array[ihigh] >= iposValue) &&  (ihigh>ilow) )
		{
			--ihigh;
		}
		itmp = array[ihigh];
		array[ihigh] = array[ilow];
		array[ilow] = itmp;
		while ((array[ilow] <= iposValue) && (ihigh > ilow))
		{
			++ilow;
		}
		itmp = array[ilow];
		array[ilow] = array[ihigh];
		array[ihigh] = itmp;
	}
	return ilow;
}
void QSort(int array[], int ilow, int ihigh)
{
	if (ilow < ihigh&&array != NULL)
	{
		int ipos = Partition(array,ilow,ihigh);
		QSort(array,ilow,ipos-1);
		QSort(array, ipos + 1, ihigh);
	}
}
void QuikSort(int array[], int icount)
{
	if (array == NULL || icount <= 0)
		return;
	QSort(array,0,icount-1);
}

//归并排序(nlogn 稳定 1、分组(一直拆) 2、归并两有序的组)

void Merge(int src[], int des[], int low, int mid, int high)
{
	if (src == NULL || des == NULL)
		return;
	int i = low, j = mid + 1;
	int k = low;
	while ((i <= mid) && j <= high)
	{
		if (src[i] <= src[j])
		{
			des[k++] = src[i++];
		}
		else
		{
			des[k++] = src[j++];
		}
	}

	while (i <= mid)
		des[k++] = src[i++];
	while (j <= high)
		des[k++] = src[j++];

}
//每次分两路,当剩下一个元素时,就不需要再划分了
void Msort(int src[], int des[], int low, int high, int max)
{
	if (src == NULL || des == NULL)
		return;
	if (low == high)
	{
		des[low] = src[low];
	}
	else //多个元素 进行两路划分
	{
		int mid = (low + high) / 2;
		std::shared_ptr<int> pSpace(new int[max]);
		//递归两路拆分
		Msort(src, pSpace,low,mid,max);
		Msort(src, pSpace,mid+1,high,max);
		Merge(pSpace,des,low,mid,high);
	}
}
//归并排序
void MergeSort(int array[], int len)
{
	Msort(array,array,0,len-1,len);
}

int main()
{
	int array[] = { 11,4,3,8,10,12,14,13,15,2,5,6,9,1,0,6,7 };
	int ilen = sizeof(array) / sizeof(array[0]);
	BubbleSort(array,ilen);
	printfarray(array, ilen);

	InsertSort(array, ilen);
	printfarray(array, ilen);

	SelectSort(array, ilen);
	printfarray(array, ilen);

	ShellSort(array, ilen);
	printfarray(array, ilen);
	
	QuikSort(array, ilen);
	printfarray(array, ilen);

	MergeSort(array, ilen);
	printfarray(array, ilen);
    std::cout << "Hello World!\n";
}

0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
0  1  2  3  4  5  6  6  7  8  9  10  11  12  13  14  15
Hello World!


C:\Users\dell\source\repos\test\Debug\test.exe (进程 20072)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值