快速排序、冒泡排序

快速排序:

int Partition(int aValue[10], int low, int high)   //完成一趟快速排序
{
    int pivot = aValue[low];

    while (low < high)
    {
        while (low < high && aValue[high] >= pivot)
        {
            high--;
        }
        int tmp = aValue[high];
        aValue[high] = aValue[low];
        aValue[low] = tmp;

        while (low < high && aValue[low] <= pivot)
        {
            low++;
        }

        tmp = aValue[high];
        aValue[high] = aValue[low];
        aValue[low] = tmp;
    }

    return low;
}

void MySort(int aValue[10], int low, int high)  //递归整个排序
{
    if (low < high)
    {
        int pivot = Partition(aValue, low, high);
        MySort(aValue, low, pivot-1);
         MySort(aValue, pivot+1, high);
    }
}

冒泡排序:

冒泡排序核心店:(1)比较的是相邻元素(2)两个for循环方向互逆(3)完成一次外层循环则排序完成一个元素(4)内层循环只是遍历剩余未排序的那一部分

方法1:从右向左(先找最大的,再次大的.....)

void bubble(int a[],int n)
{ 
	int i,j,t;
    // 从右向左,每一轮循环得到剩余未排序最大值放到i位置上
	for(i=n-1;i>0;--i)
	{
        // 从远处(从左向右)相邻比较交换,循环结束后,剩余未排序最大值放到了i位置上
		for(j=0;j<i;++j)
		{
			if(a[j]>a[j+1])
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
}

方法2:从左到右(先找最小的,再次小的.....)

void bubble1(int a[],int n)
{ 
	int i,j,t;

	// 从左向右,每一轮循环得到剩余未排序最小值放到i位置上
	for(i = 0; i < n-1; i++)
	{
		// 从远处(从右向左)相邻比较交换,循环结束后,剩余未排序最小值放到了i位置上
		for(j = n-1; j > i; j--)
		{
			if(a[j]<a[j-1])
			{
				t=a[j];
				a[j]=a[j-1];
				a[j-1]=t;
			}
		}

		print(a,n);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值