数据结构与算法(C++实现)学习笔记(二)

数据结构与算法(C++实现)学习笔记(二)

这次主要学习了顺序查找、二分查找、递归以及递归的二分法查找。

顺序查找 SequentialSearch(int *a, const int n, int x)

ps: 在含有n个数据的数组a中查找x。

(1)顺序查找适合于没有排序的数据

(2)顺序查找:速度慢

代码示例:

#include<iostream>
using namespace std;
int SequentialSearch(int *a, const int n, int x);
int main()
{
	int m[] = { 2,4,6,8,0,1,3,5,7,9 };
	int num = 5;
	int result;
	result = SequentialSearch(m, 10, num);
	if (result < 0)
		cout << "not found" << endl;
	else
		cout << "m[" << result << "] is " << num << endl;
	return 0;
}
int SequentialSearch(int *a, const int n, int x)
{
	int i;
	for (int i = 0; i < n; i++){
		if (a[i] == x)
			return i;
	}
	if (i == n)  return -1; //c++中,此判断语句可以省略
}

 

折半查找(二分查找)BinarySearch(int *a, const int n,const int x)

要点:二分法查找适用于数据量较大时,但是数据需要先排好顺序。每一次查找与中间值比较,可以确定是否查找成功,不成功当前查找区间将缩小一半,递归查找即可。速度快!

100万个数据大概要查找20次,10亿个数据要查找30次左右。(2^{20}\approx 100 00002^{^30}\approx 10亿)

图解二分法:选取12个已经排好序的数组,查找701的过程。

代码示例:

#include<iostream>
using namespace std;
int BinarySearch(int *a, const int n, int x);
int main()
{
	int m[] = { 0,1,2,3,4,5,6,7,8,9};
	int num = 75;
	int result;
	result = BinarySearch(m, 10, num);
	if (result < 0)
		cout << "not found" << endl;
	else
		cout << "m[" << result << "] is " << num << endl;
	return 0;
}
int BinarySearch(int *a, const int n, int x)
{
	int low, mid, high;
	low = 0;
	high = n - 1;
	while (low < high) {
		mid = (low + high) / 2;
		if (a[mid] == x)
			return mid;
		else if (a[mid] < x)
			low = mid + 1;
		else 
			high = mid - 1;
	}
	return -1;
}

递归

俗语云:递归是神,迭代(循环)是人。学好递归,可以让程序看起来十分的简洁。

优劣:迭代使得程序看起来简洁,但是速度慢一些,内存占用也会多一些。

一个例子:从前有座山,山上有座庙,庙里有老和尚和小和尚,老和尚给小和尚讲故事:“从前有座山……”

代码示例:计算阶乘(递归VS迭代)

#include<iostream>
using namespace std;
int factorial(int n)
{
	if (n == 0)
		return 1;
	else
		return n * factorial(n - 1);
}
int factorial2(int n)
{
	long result = 1;
	for (int i = n; i > 0; i--)
		result = result * i;
	return result;
}
int main()
{
	int num = 10;
	for (int i = 0; i < num; i++) {
		cout << "递归: " << i << "! = " << factorial(i) << endl;
		cout << "迭代: " << i << "! = " << factorial2(i) << endl;
	}
	return 0;
}

迭代的二分法查找

代码示例

#include<iostream>
using namespace std;
int BinarySearch(int *a, const int x, const int left, const int right);
int main()
{
	int m[] = { 0,1,2,3,4,5,6,7,8,9 };
	int result;
	int num = 5;
	result = BinarySearch(m, num, 0, 10);
		if (result < 0)
			cout << "not found" << endl;
		else
			cout << "m[" << result << "] is " << num << endl;
	return 0;
}
int BinarySearch(int *a, const int x, const int left, const int right)
{
	if (left <= right) {
		int middle = (left + right) / 2;
		if (a[middle] < x)		return BinarySearch(a, x, middle + 1, right);
		else if (a[middle] > x)		return BinarySearch(a, x, left, middle - 1);
		else return middle;
	}
	return -1;
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值