用“查找”这一章的各种查找算法(顺序查找、折半查找、Hash查找[自己建立装填因子尽可能大的Hash表])实现上述已排序的20 000个数据中第12345个数据的查找(如果是C语言,则数组形式的数组下标为01234;即把已排序数据的第12345个数据作为key值,然后再把已排序序列当作未知具体数值的序列按相应算法进行查找,返回查找到的位置,并计算该过程耗费的时间或实际关键字的比较次数),比较不同算法的实际运行时间或经过的比较次数
时间: 2024-02-19 21:58:50 浏览: 87
好的,我明白了你的要求。以下是使用不同算法进行查找的伪代码:
顺序查找:
```
pos = -1
for i = 0 to len(data)-1 do:
if data[i] == key:
pos = i
break
```
折半查找:
```
pos = -1
low = 0
high = len(data)-1
while low <= high do:
mid = (low + high) // 2
if data[mid] == key:
pos = mid
break
elif data[mid] < key:
low = mid + 1
else:
high = mid - 1
```
Hash查找:
```
pos = -1
hash_table = build_hash_table(data)
hash_val = hash(key)
if hash_table[hash_val] is not None and hash_table[hash_val] == key:
pos = hash_val
```
其中,`build_hash_table` 是一个函数,用于构建哈希表;`hash` 是一个函数,用于计算哈希值。
为了比较不同算法的实际运行时间或经过的比较次数,我需要实际运行这些算法,并记录运行时间和比较次数。由于我是一个AI,我无法直接在本地计算机上进行运算,因此我建议你使用一些在线编程平台,如Google Colab或CodeSandbox等,来运行这些算法并记录结果。你可以将代码和数据上传到这些平台上,然后运行并记录结果。另外,请提供你的数据是以什么样的形式存储的,以及你的机器的硬件配置情况:CPU物理核心数、单核频数,内存容量。这些信息将有助于我更好地帮助你。
阅读全文
相关推荐
















