查找算法之顺序搜索
2017-10-18 15:27
330 查看
顺序查找的思路:
设A[1..n]为一个n个元素的数组,判定给定元素x是否在A中,顺序查找的思路如下:扫描A中的所有元素,将每个元素与x比较,如果在j次比较后(1<=j<=n)搜索成功,即x=A[j],则返回j的值,否则返回-1,表示没有找到。
顺序查找的实现:
/** * 顺序搜索 * Created by yuzhan on 2017/10/18. */ public class main { public static int LinearSearch(int[] A, int x){ int j = 0; int n = A.length; while(j < n && x != A[j]){ j++; } if(j < n) return j; else return -1; } public static void main(String[] args) { int[] A = {2,1,3,6,4,23,65,75,34,67,32}; int x = 32; int index = main.LinearSearch(A,x); System.out.print(index); } }
相关文章推荐
- 算法 杨氏矩阵搜索(二维数组中的查找)
- Java经典算法汇总之顺序查找(Sequential Search)
- 【数据结构】算法9.1 顺序表查找-顺序查找
- 实现顺序查找的算法
- java算法系列之(二)顺序查找算法
- PHP算法之二分查找和顺序查找
- 《Delphi 算法与数据结构》学习与感悟[1]: 通过 "顺序查找" 与 "二分查找" 说明算法的重要性
- 一步一步复习数据结构和算法基础-索引顺序表查找
- 【数据结构与算法】【查找】顺序查找(线性查找)的代码实现
- 算法易错点:二分代码/奇偶调顺序/查找第一次出现的字符
- 算法与数据结构--在顺序线性表L中查找第1个值与e满足compare()的元素的为序--算法2.5
- 带监视哨和没有带监视哨的顺序查找算法
- 算法复习--------------基本算法:顺序查找,选择排序,冒泡排序算法等简单算法
- 算法学习之查找(顺序、二分法、排序二叉树以及 Hash 表)
- C++11写算法之顺序查找
- 数据查找算法---顺序查找、二分法查找、二叉树查找、hash查找
- 第九章中位数和顺序统计学之“查找第i小的元素(递归版)平均运行时间为O(n)算法”
- 顺序表创建以及查找排序算法(含有顺序查找算法、带哨兵站顺序查找、折半查找算法、冒泡排序)的C++实现在vs2013环境下实现
- C++版基本算法1--顺序查找
- 【数据结构----笔记1】查找算法之【顺序查找和折半查找】