二分查找
二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法,前提是数据对象必须先排好序。二分查找事实上采用的是一种“分治”策略,它充分利用了元素间的次序关系。
代码演示:
#include <iostream>
using namespace std;
//可以递归调用的二分查找
int search(const int(&a)[10], int start, int end, int target) {
//基准情况:目标值超出范围,或者start > end 说明没有找到
if (target < a[start] || target > a[end] || start > end)
return -1;
//计算中间坐标
int mid = (start + end) / 2;
//比较中间值和目标值的大小
if (a[mid] == target)
return mid;
else if (a[mid] > target)
return search(a, start, mid - 1, target); //比较目标值大, 在更小的部分找
else
return search(a, mid + 1, end, target); //比较目标值小, 在更大的部分找
}
int main()
{
int arr[10] = { 1, 2, 3, 4, 5 ,6, 7 ,8, 9, 10 };
int key = 8;
int size = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, 0, size - 1, key);
result == -1
? cout << "没有找到值" << key << "!" << endl
: cout << "在数组找到值" << key << ",索引下标值为:" << result << endl;
cin.get();
}
输出结果:
快速排序
快速排序是一种高效的排序算法,也应用了分治思想,通过一次扫描,将待排记录分隔成独立的两部分,其中一部分的值全比另一部分的小;接下来分别对这两部分继续进行排序,最终全部排完。
代码演示:
#include <iostream>
using namespace std;
void quickSort(int(&)[10], int, int);
int partition(int(&)[10], int, int);
void printfArr(const int(&)[10]);
void swap(int(&)[10], int, int);
int main()
{
int arr[10] = { 23, 45, 18, 6, 11, 19, 22, 18, 12, 9 };
printfArr(arr);
int size = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, size - 1);
printfArr(arr);
cin.get();
}
void quickSort(int(&a)[10], int start, int end)
{
//基准情况
if (start >= end)
return;
//分区,返回分区点的下标
int mid = partition(a, start, end);
//递归调用, 分别对两部分继续排序
quickSort(a, start, mid - 1);
quickSort(a, mid + 1, end);
}
//按照pivot分区的函数
int partition(int(&a)[10], int start, int end)
{
//选取一个分区的“支点”
int pivot = a[start];
//分区左右支点
int left = start, right = end;
while (left < right)
{
//分别从左右两边遍历数组
while (a[left] <= pivot && left < right)
++left;
while (a[right] >= pivot && left < right)
--right;
//左右互换
swap(a, left, right);
}
if (a[left] < pivot)
{
swap(a, start, left);
return left;
}
else if (a[left] > pivot)
{
swap(a, start, left - 1);
return left -1;
}
}
void swap(int(&a)[10], int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
//打印输出函数
void printfArr(const int(&a)[10]) {
for (int num : a)
cout << num << "\t";
cout << endl;
}
输出结果:
二叉树
跟数组不同,树是一种非线性的数据结构,是由n(n >=0)个节点组成的有限集合。如果n==0,树为空树。如果n>0,树有一个特定的节点,叫做根节点(root)。
对于树这种数据结构,使用最频繁的是二叉树。每个节点最多只有2个子节点的树,叫做二叉树。二叉树中,每个节点的子节点作为根的两个子树,一般叫做节点的左子树和右子树。
我们可以为树的节点定义一种结构体类型,而且为了方便以后在不同的文件中使用,还可以自定义一个头文件tree_node.h,将结构体TreeNode的定义放在里面:
#pragma once
#include<string>
using namespace std;
struct TreeNode
{
string name;
TreeNode* left;
TreeNode* right;
};
void printTreePreOrder(TreeNode* root);
void printTreeInOrder(TreeNode* root);
void printTreePosOrder(TreeNode* root);
对于树的遍历,主要有这样三种方式:
- 先序遍历:先访问根节点,再访问左子树,最后访问右子树;
- 中序遍历:先访问左子树,再访问根节点,最后访问右子树;
- 后序遍历:先访问左子树,再访问右子树,最后访问根节点。
这种遍历方式就隐含了“递归”的思路:左右子树本身又是一棵树,同样需要按照对应的规则来遍历。
我们可以先单独创建一个文件"print_tree.cpp",实现二叉树的遍历方法:
#include <iostream>
#include "tree_node.h"
using namespace std;
//利用递归实现二叉树的先序遍历打印输出
void printTreePreOrder(TreeNode* root)
{
//基准情况:如果为空树直接返回
if (root == nullptr) return;
/* 先打印节点的值
ఠ ﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏ ఠ
\ cout <<(*root).name << "\t"; /
( ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄/ ൠ \ ̄ ̄ ̄ ̄ ̄ ̄ ̄) */
cout << root->name << "\t";
//递归打印左右树
printTreePreOrder(root->left);
printTreePreOrder(root->right);
}
//中序遍历
void printTreeInOrder(TreeNode* root)
{
//基准情况:如果为空树直接返回
if (root == nullptr) return;
//先打印左子树
printTreeInOrder(root->left);
//打印根节点
cout << root->name << "\t";
//递归打印左右树
printTreeInOrder(root->right);
}
//后序遍历
void printTreePosOrder(TreeNode* root)
{
//基准情况:如果为空树直接返回
if (root == nullptr) return;
//先递归打印左右树
printTreePosOrder(root->left);
printTreePosOrder(root->right);
//再打印根节点
cout << root->name << "\t";
}
接下来就可以在"traversal_tree.cpp"文件中实现具体的功能了:
#include <iostream>
#include "tree_node.h"
using namespace std;
int main()
{
//定义一颗二叉树
TreeNode nodeG = { "G", nullptr, nullptr };
TreeNode nodeF = { "F", nullptr, nullptr };
TreeNode nodeE = { "E", &nodeG, nullptr };
TreeNode nodeD = { "D", nullptr, nullptr };
TreeNode nodeC = { "C", nullptr, &nodeF };
TreeNode nodeB = { "B", &nodeD, &nodeE };
TreeNode nodeA = { "A", &nodeB, &nodeC };
TreeNode* tree = &nodeA;
printTreePreOrder(tree);
cout << endl << endl;
printTreeInOrder(tree);
cout << endl << endl;
printTreePosOrder(tree);
cin.get();
}
输出结果: