1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-06-16 06:56:54 +08:00

更新查找

This commit is contained in:
Didnelpsun
2021-09-28 23:19:15 +08:00
parent 7456b03e9a
commit 03eec49ec2
6 changed files with 244 additions and 59 deletions

34
Code/Code/head/search.h Normal file
View File

@@ -0,0 +1,34 @@
#include "head.h"
typedef int elem_type
// 线性表
typedef struct {
elem_type *elem;
int length;
} LinearTable;
// 顺序查找
int SequenceSearch(LinearTable table, elem_type key) {
for (int i = 0; i < table.length; i++) {
if (table.elem[i] == key)
return i;
}
return -1;
}
// 折半查找
int BinarySearch(LinearTable table, elem_type key) {
int low = 0, high = table.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (table.elem[mid] == key)
return mid;
else if (table.elem[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}

View File

@@ -15,6 +15,7 @@
#include "../head/thread_tree.h"
#include "../head/tree.h"
#include "../head/graph.h"
#include "../head/search.h"
using namespace std;