排序

冒泡排序

for (i = 0; i < N; i++)//从小到大
	{
		for (j = 0; j < N - i; j++)
		{
			if (a[j] < a[j + 1])
			{
				tmp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = tmp;
			}
		}
	}

折半查找

int Search(int a[],int low,int high,int key)
{
    int mid;
    while(low<=high)
    {
        mid = (low+high)/2;
        if(a[mid]==key) //查找到
        {
            return mid;
        }
        else if(a[mid]<key)  //中间位置的关键字小于要查找的关键字
        {
            low = mid+1;
        }
        else  //中间位置的关键字大于要查找的关键字
        {
            high = mid-1;
        }
    }
    return -1;
}

the end……

发表评论