快排,二分查找

// search_Bin.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

int binSearch(int a[6], int x, int len );

int posFun(int * a, int low, int heigh);


int binSearch(int a[6], int x, int len ){

	int low = 0;
	int heigh = len - 1;
	int mid;

	while (low <= heigh)
	{
		mid = (low + heigh)/2; 
		if (a[mid] < x)
		{
			low = mid + 1;
		}else{
			if(a[mid] > x){
				heigh = mid - 1;
			}else{
				return mid;
			}
		}
	}

	return -100;


}

void quickSort(int a[], int low, int heigh){

	int positacion = 0;
	positacion = posFun(a, low, heigh);

	if (low < heigh)
	{
		quickSort(a, low, positacion -1);
		quickSort(a, positacion + 1, heigh);
	}
}

int posFun(int * a, int low, int heigh){

	int var = a[low];

	//while循环退出的条件是 low == heigh 这点要注意
	while (low < heigh)
	{

		while (low < heigh && (a[heigh] > var ))
			--heigh;
		a[low] = a[heigh];

		while (low < heigh && a[low] < var)
			++low;
		a[heigh] = a[low];

	}

	a[low] = var;

	return low;

}

int _tmain(int argc, _TCHAR* argv[])
{
	int a[6] = {1, 2, 3, 4, 5, 6};

	int resultVal;

	resultVal = binSearch(a, 4, 6);

	printf("the val is %d", resultVal);

	int b[6] = {3, 1, 4, 8, 9};

	quickSort(b, 0, 5);

	
	for (int i = 0; i < 5; i++)
	{
		printf("the val is %d", b[i]);
	}

	return 0;
}

void BubbleSort(int x[],int n)
{
	int i,j;
	bool Exchange;  //记录交换标志
	for(int i=1;i < n - 1; i++)    //最多做n-1趟排序
	{
		Exchange = false;
		for(j=n-1;j>=i;--j)
		{
			if(x[j]>x[j+1])
			{
				x[0] = x[j];
				x[j] = x[j+1];
				x[j+1] = x[0];
				Exchange = true;   //发生了交换,设置标志为真.
			}
		}
		if (!Exchange )      //为发生替换,提前终止算法
			return;

	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值