已知有20个学生记录(包括学号,姓名,成绩等) 设计三个查找算法,分别实现按学号、姓名、成绩的顺序查找; 菜单选择查找方式;
时间: 2024-02-03 15:12:20 浏览: 128
用文件存储10个学生的姓名、学号、手机号、地址,可通过姓名或者学号查找
5星 · 资源好评率100%
可以使用以下三种查找算法实现按学号、姓名、成绩的顺序查找:
1. 顺序查找
顺序查找(Sequential Search)是一种简单的查找算法,它从数据的起始位置开始依次比较每个元素,直到找到目标元素或搜索到数据的末尾为止。
实现按学号顺序查找的代码示例:
```python
def search_by_id(students, target_id):
for student in students:
if student['id'] == target_id:
return student
return None
```
实现按姓名顺序查找的代码示例:
```python
def search_by_name(students, target_name):
for student in students:
if student['name'] == target_name:
return student
return None
```
实现按成绩顺序查找的代码示例:
```python
def search_by_score(students, target_score):
for student in students:
if student['score'] == target_score:
return student
return None
```
2. 二分查找
二分查找(Binary Search)是一种高效的查找算法,它要求待查找的数据必须有序,每次查找可以将数据范围缩小一半。
实现按学号顺序查找的代码示例:
```python
def binary_search_by_id(students, target_id):
left = 0
right = len(students) - 1
while left <= right:
mid = (left + right) // 2
if students[mid]['id'] == target_id:
return students[mid]
elif students[mid]['id'] < target_id:
left = mid + 1
else:
right = mid - 1
return None
```
实现按姓名顺序查找的代码示例:
```python
def binary_search_by_name(students, target_name):
left = 0
right = len(students) - 1
while left <= right:
mid = (left + right) // 2
if students[mid]['name'] == target_name:
return students[mid]
elif students[mid]['name'] < target_name:
left = mid + 1
else:
right = mid - 1
return None
```
实现按成绩顺序查找的代码示例:
```python
def binary_search_by_score(students, target_score):
left = 0
right = len(students) - 1
while left <= right:
mid = (left + right) // 2
if students[mid]['score'] == target_score:
return students[mid]
elif students[mid]['score'] < target_score:
left = mid + 1
else:
right = mid - 1
return None
```
3. 哈希查找
哈希查找(Hash Search)是一种基于哈希表的查找算法,它利用哈希函数将查找的关键字映射到哈希表中的一个位置,并在该位置查找目标元素。
实现按学号顺序查找的代码示例:
```python
def hash_search_by_id(students, target_id):
hash_table = {}
for student in students:
hash_table[student['id']] = student
return hash_table.get(target_id)
```
实现按姓名顺序查找的代码示例:
```python
def hash_search_by_name(students, target_name):
hash_table = {}
for student in students:
hash_table[student['name']] = student
return hash_table.get(target_name)
```
实现按成绩顺序查找的代码示例:
```python
def hash_search_by_score(students, target_score):
hash_table = {}
for student in students:
hash_table[student['score']] = student
return hash_table.get(target_score)
```
可以使用以下代码实现菜单选择查找方式:
```python
students = [{'id': 1, 'name': '张三', 'score': 90},
{'id': 2, 'name': '李四', 'score': 80},
{'id': 3, 'name': '王五', 'score': 70},
{'id': 4, 'name': '赵六', 'score': 60},
{'id': 5, 'name': '钱七', 'score': 50},
{'id': 6, 'name': '孙八', 'score': 40},
{'id': 7, 'name': '周九', 'score': 30},
{'id': 8, 'name': '吴十', 'score': 20},
{'id': 9, 'name': '郑一', 'score': 10},
{'id': 10, 'name': '刘二', 'score': 100},
{'id': 11, 'name': '陈三', 'score': 95},
{'id': 12, 'name': '黄四', 'score': 85},
{'id': 13, 'name': '林五', 'score': 75},
{'id': 14, 'name': '周六', 'score': 65},
{'id': 15, 'name': '刘七', 'score': 55},
{'id': 16, 'name': '杨八', 'score': 45},
{'id': 17, 'name': '吴九', 'score': 35},
{'id': 18, 'name': '郑十', 'score': 25},
{'id': 19, 'name': '孙一', 'score': 15},
{'id': 20, 'name': '张二', 'score': 5}]
while True:
print('请选择查找方式:')
print('1. 按学号查找')
print('2. 按姓名查找')
print('3. 按成绩查找')
print('4. 退出程序')
choice = input('请输入选项:')
if choice == '1':
target_id = int(input('请输入要查找的学号:'))
result = binary_search_by_id(students, target_id)
if result:
print(result)
else:
print('未找到该学生记录')
elif choice == '2':
target_name = input('请输入要查找的姓名:')
result = binary_search_by_name(students, target_name)
if result:
print(result)
else:
print('未找到该学生记录')
elif choice == '3':
target_score = int(input('请输入要查找的成绩:'))
result = binary_search_by_score(students, target_score)
if result:
print(result)
else:
print('未找到该学生记录')
elif choice == '4':
break
else:
print('输入有误,请重新输入')
```
阅读全文