编写顺序查找算法。 int SeqSearch(STable ST, KeyType key)
时间: 2024-05-18 10:12:19 浏览: 14
好的,以下是顺序查找算法的代码实现:
```
typedef struct {
KeyType key;
// 其他数据项
} ElemType;
typedef struct {
ElemType *elem;
int length;
} STable;
int SeqSearch(STable ST, KeyType key) {
ST.elem[0].key = key; // 哨兵
int i;
for (i = ST.length; ST.elem[i].key != key; i--);
return i; // 返回查找到的元素下标,未找到返回0
}
```
其中,`STable` 为顺序表数据结构,`ElemType` 为数据元素类型,包含 `key` 和其他数据项。`SeqSearch` 函数接收一个 `STable` 类型的顺序表和一个 `KeyType` 类型的关键字 `key`,返回查找到的元素下标,未找到返回0。具体实现是利用一个哨兵,将 `key` 值放在数组的最后一位,这样在循环中就不需要每次判断是否到达数组末尾。从数组末尾开始往前查找,找到第一个与 `key` 相等的元素即可。
相关问题
c++编程实现顺序查找算法及二分法查找算法
以下是C++实现顺序查找算法和二分法查找算法的示例代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 顺序查找算法
int seqSearch(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == target) {
return i;
}
}
return -1; // 查找失败
}
// 二分法查找算法
int binarySearch(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1; // 查找失败
}
int main() {
vector<int> nums = {3, 5, 2, 8, 4, 7, 1, 6};
sort(nums.begin(), nums.end()); // 二分法查找算法需要有序表
int target = 4;
int index1 = seqSearch(nums, target); // 调用顺序查找算法
int index2 = binarySearch(nums, target); // 调用二分法查找算法
if (index1 != -1) {
cout << "顺序查找成功,目标元素下标为:" << index1 << endl;
}
else {
cout << "顺序查找失败,未找到目标元素" << endl;
}
if (index2 != -1) {
cout << "二分法查找成功,目标元素下标为:" << index2 << endl;
}
else {
cout << "二分法查找失败,未找到目标元素" << endl;
}
return 0;
}
```
以上代码中,我们使用了STL中的vector容器来存储顺序表元素,并使用sort函数对其进行排序,以便二分法查找算法能够正确执行。在main函数中,我们分别调用了顺序查找算法和二分法查找算法,查找目标元素的值为4。最后,根据返回的下标值,输出查找结果。
dev-c++编程实现顺序查找算法及二分法查找算法
以下是Dev-C++实现顺序查找算法和二分法查找算法的示例代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 顺序查找算法
int seqSearch(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == target) {
return i;
}
}
return -1; // 查找失败
}
// 二分法查找算法
int binarySearch(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1; // 查找失败
}
int main() {
vector<int> nums = {3, 5, 2, 8, 4, 7, 1, 6};
sort(nums.begin(), nums.end()); // 二分法查找算法需要有序表
int target = 4;
int index1 = seqSearch(nums, target); // 调用顺序查找算法
int index2 = binarySearch(nums, target); // 调用二分法查找算法
if (index1 != -1) {
cout << "顺序查找成功,目标元素下标为:" << index1 << endl;
}
else {
cout << "顺序查找失败,未找到目标元素" << endl;
}
if (index2 != -1) {
cout << "二分法查找成功,目标元素下标为:" << index2 << endl;
}
else {
cout << "二分法查找失败,未找到目标元素" << endl;
}
return 0;
}
```
以上代码中,我们使用了STL中的vector容器来存储顺序表元素,并使用sort函数对其进行排序,以便二分法查找算法能够正确执行。在main函数中,我们分别调用了顺序查找算法和二分法查找算法,查找目标元素的值为4。最后,根据返回的下标值,输出查找结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)