1、顺序查找:O(n)

//1、顺序查找
int SequentialSearch(int *array, int n, int key)
{
    int i=0;
    while( i < n  &&  array[i] != key)
    {
        ++i;
    }
    if (array[i] == key)
    {
        cout<<"Find: ";
        cout<<i;
        return i;
    }
    else 
    {
        cout<<"Not Find!";
        return -1;
    }
}

int SequentialSearchAdvanced(int *array, int n, int key)
{
    int i=0;
    while(array[i] != key)
    {
        ++i;
    }
    if (i < n)
    {
        cout<<"Find: ";
        cout<<i;
        return i;
    }
    else 
    {
        cout<<"Not Find!";
        return -1;
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-13
  • 2021-06-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2021-12-16
相关资源
相似解决方案