21年考研王道数据结构2-9算法:通过折半查找指定值X并根据结果进行操作(两种方法)

题目:线性表中的元素递增有序且按照顺序存储在计算机中,要求设计一种算法在最少时间内查找到数值为X的元素,若找到则将其与后继元素位置交换,若找不到则将其插入表中使表中元素仍递增有序

分析:要求最少时间则采用折半查找,分为递归和循环两种。若找到元素后该元素位置为最后一个则不做处理,若找不到该元素后插入该元素到数组中必会引起指针错误,因为数组大小为a[n],插入后变成a[n+1]引发内存未初始化的错误,所以要挤掉数组最后一个元素后再插入

递归方法#include <iostream>

using namespace std;

int ch(int a[], int left, int right, int x,int len)
{
    if (left > right) {
        cout<<"left>right! break;"<<endl;
            return -1;
    }
    //system("pause");
    cout <<"now is "<<(left+right)/2<<", a[i]= "<<a[(left+right)/2]<< ", left= " << left << 
", right= " << right << ", x= " << x <<", len="<<len<< endl; if (x == a[(left + right) / 2]&&((left + right) / 2+1 !=len)){
//找到X,排除掉X为最后一个元素的情况后开始和后一个元素交换位置 cout << "index=" << (left + right) / 2 + 1 << endl; int temp = a[(left + right) / 2]; a[(left + right) / 2] = a[(left + right) / 2 + 1];
//不排除上述的情况话,会在此处出现内存错误,
//因为a[(left+right)/2]=a[9]=19,a[(left+right)/2+1]=a[10]未定义
a[(left + right) / 2 + 1] = temp; return 1; } else if (x != a[(left + right) / 2] && left == right) {
//未找到X,开始插入 cout << "not found!" << endl; for (int i = len-1;i > (left + right) / 2; i--)
//这里i不能是len=10,因为同样a[10]无定义 { a[i]
= a[i - 1]; } a[(left + right) / 2] = x; return 0; } else if (x > a[(left + right) / 2]) return ch(a, (left + right) / 2+1, right, x,len); else if (x < a[(left + right) / 2]) return ch(a, left, (left + right) / 2-1, x,len); } int main() { int a[] = { 1,3,5,7,9,11,13,15,17,19 }; cout << "sizeof(a)/sizeof(a[0])= " << sizeof(a) / sizeof(a[0]) << " sizeof(a)=" << sizeof(a) << endl; cout << "result=" << ch(a,0,9,0,sizeof(a)/sizeof(a[0])) << endl; for (auto i : a) cout << " " << i; }

循环方法:

猜你喜欢

转载自www.cnblogs.com/murenma/p/13200874.html