标签:sha 设置 csharp else 折半查找 str 顺序查找 seq har
顺序查找
typedef struct { KeyType key; InfoType data; }RecType;
int SeqSearch(RecType R[], int n, KeyType k) { int i = 0; while (i < n && R[i].key != k) i++; if (i >= n) return 0; else return i + 1; }
设置哨兵,防止访问越界
int SeqSearch1(RecType R[], int n, KeyType k) { int i = 0; R[n].key = k; while (R[i].key != k) i++; if (i == n) return 0; else return i + 1; }
折半查找
int BinSearch(RecType R[], int n, KeyType k) { int low = 0, high = n - 1, mid; while (low <= high) { mid = (low + high) / 2; if (R[mid].key == k) return mid + 1; else if (R[mid].key < k) low = mid + 1; else high = mid - 1; } return 0; }
标签:sha 设置 csharp else 折半查找 str 顺序查找 seq har
原文地址:https://www.cnblogs.com/KIROsola/p/11622402.html