排序

#include <iostream>

using namespace std;

void QuickSort(int a[], int low, int high)//0--- n-1
{
	int i, j, flagNum;
	if(low < high)
	{
		i = low;
		j = high;
		flagNum = a[low];
		while(i < j)
		{
			while(i < j && a[j] >= flagNum)
				j--;
			if(i < j)
				a[i++] = a[j];
			while(i < j && a[i] <= flagNum)
				i++;
			if(i < j)
				a[j--] = a[i];
		}
		a[j] = flagNum;
		QuickSort(a, low, j-1);
		QuickSort(a, j+1, high);
	}
}

void bubbleSort(int a[], int n)
{
	for(int i = 0; i <= n;i++)
	{
		for(int j = n; j > i ;j--)
		{
			if(a[j] <= a[j-1])
			{
				int temp = a[j];
				a[j] = a[j-1];
				a[j-1] = temp;
			}
		}	
	}
}

int InsertSort(int a[], int n)
{
	int i, j,temp;
	for(i = 1; i <= n; i++)
	{
		temp = a[i];
		for(j = i-1; j >=0 && temp <= a[j]; j--)
		{
			a[j+1] = a[j];
		}
		a[j+1] = temp;
	}	
}

int SelectSort(int a[], int n)
{
	int i, j;
	for(i = 0;i <= n; i++)
	{
		for(j = i+1; j <= n; j++)
		{
			if(a[i] >= a[j])
			{
				int temp = a[i];
				a[i] = a[j];
				a[j] = temp;	
			}
		}
	}
}

int ShellSort(int a[], int n)
{
	int i, h, j, temp;
	for(h = n/2; h > 0; h = h/2)
	{
		for(i = h; i <= n; i++)
		{
			temp = a[i];
			for(j = i-h; j >=0 && temp <= a[j]; j = j-h)
			{
				a[j+h] = a[j];
			}
			a[j+h] = temp;
		}
	}
}

int main()
{
	int a[10] = {1, 2, 5, 3, 4, 6, 8, 7, 10, 9};
	//QuickSort(a, 0, 9);
	//bubbleSort(a, 9);
	//InsertSort(a, 9);
	//SelectSort(a, 9);
	ShellSort(a, 9);
	for(int i = 0; i < 10; i++)
	{
		cout << a[i] << " ";
	}
	cout <<endl;
	
	return 0;
}

  

posted on 2015-10-26 16:03 BeatificDevin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/devinblog/p/4911487.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值