mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 06:56:54 +08:00
更新查找
This commit is contained in:
34
Code/Code/head/search.h
Normal file
34
Code/Code/head/search.h
Normal 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;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "../head/thread_tree.h"
|
||||
#include "../head/tree.h"
|
||||
#include "../head/graph.h"
|
||||
#include "../head/search.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user