添加二分查找(折半查找)、TCP状态码

This commit is contained in:
huihut 2018-03-10 17:45:53 +08:00
parent f36e69d20f
commit 3ea3284547
3 changed files with 64 additions and 9 deletions

View File

@ -6,6 +6,11 @@
"xtree": "cpp",
"xutility": "cpp",
"iosfwd": "cpp",
"vector": "cpp"
"vector": "cpp",
"algorithm": "cpp",
"streambuf": "cpp",
"tuple": "cpp",
"system_error": "cpp",
"xtr1common": "cpp"
}
}

37
Algorithm/BinarySearch.h Normal file
View File

@ -0,0 +1,37 @@
// 二分查找(折半查找):对于已排序,若无序,需要先排序
// 非递归
int BinarySearch(vector<int> v, int value)
{
if (v.size() <= 0)
return -1;
int low = 0;
int high = v.size() - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (v[mid] == value)
return mid;
else if (v[mid] > value)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
// 递归
int BinarySearch2(vector<int> v, int value, int low, int high)
{
if (low > high)
return -1;
int mid = low + (high - low) / 2;
if (v[mid] == value)
return mid;
else if (v[mid] > value)
return BinarySearch2(v, value, low, mid - 1);
else
return BinarySearch2(v, value, mid + 1, high);
}

View File

@ -977,17 +977,22 @@ typedef struct BiTNode
### 排序
* [冒泡排序](Algorithm/BubbleSort.h)
* [冒泡排序(改进版)](Algorithm/BubbleSort_orderly.h)
* [选择排序](Algorithm/SelectionSort.h)
* [快速排序](Algorithm/QuickSort.h)
* [文件排序](Algorithm/FileSort)
排序算法 | 平均时间复杂度 | 最差时间复杂度 | 空间复杂度 | 稳定性
---|---|---|---|---
[冒泡排序](Algorithm/BubbleSort.h) | O(n<sup>2</sup>)|O(n<sup>2</sup>)|O(1)|稳定
[冒泡排序(改进版)](Algorithm/BubbleSort_orderly.h) | O(n<sup>2</sup>)|O(n<sup>2</sup>)|O(1)|稳定
[选择排序](Algorithm/SelectionSort.h) | O(n<sup>2</sup>)|O(n<sup>2</sup>)|O(1)|稳定
[快速排序](Algorithm/QuickSort.h) | O(n*log<sub>2</sub>n) | O(n<sup>2</sup>) | O(log<sub>2</sub>n)~O(n) | 不稳定
[文件排序](Algorithm/FileSort) |
### 查找
* [顺序查找](Algorithm/SequentialSearch.h)
* [蛮力字符串匹配](Algorithm/BruteForceStringMatch.h)
* [文件查找](Algorithm/FileSearch)
查找算法 | 平均时间复杂度 | 空间复杂度 | 查找条件
---|---|---|---
[顺序查找](Algorithm/SequentialSearch.h) | O(n) | O(1) | 无序或有序
[二分查找(折半查找)](Algorithm/BinarySearch.h) | O(log<sub>2</sub>n)| O(1) | 有序
[蛮力字符串匹配](Algorithm/BruteForceStringMatch.h) | O(n*m) |
[文件查找](Algorithm/FileSearch) |
## Problems
@ -1163,6 +1168,14 @@ TCP 首部
![TCP首部](images/TCP首部.png)
TCP状态控制码CodeControl Flag占6比特含义如下
* URG紧急比特urgent当URG1时表明紧急指针字段有效代表该封包为紧急封包。它告诉系统此报文段中有紧急数据应尽快传送(相当于高优先级的数据) 且上图中的 Urgent Pointer 字段也会被启用。
* ACK: 确认比特Acknowledge。只有当ACK1时确认号字段才有效代表这个封包为确认封包。当ACK0时确认号无效。
* PSH: Push function若为1时代表要求对方立即传送缓冲区内的其他对应封包而无需等缓冲满了才送。
* RST: 复位比特(Reset)当RST1时表明TCP连接中出现严重差错如由于主机崩溃或其他原因必须释放连接然后再重新建立运输连接。
* SYN: 同步比特(Synchronous)SYN置为1就表示这是一个连接请求或连接接受报文通常带有 SYN 标志的封包表示『主动』要连接到对方的意思。
* FIN: 终止比特(Final)用来释放一个连接。当FIN1时表明此报文段的发送端的数据已发送完毕并要求释放运输连接。
#### UDP
* UDPUser Datagram Protocol用户数据报协议是OSIOpen System Interconnection 开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,其传输的单位是用户数据报。