算法-->顺序查找
2017-07-22 22:50
232 查看
package 顺序查找; import java.util.Scanner; public class ShunXu { static final int N = 15; static int searchFun(int a[], int n, int x) { int i, f = -1; for (i = 0; i < n; i++) { if (x == a[i]) { f = i; break; } } return f; } public static void main(String[] args) { int x, n, i; ShunXu sh = new ShunXu(); int[] shuzu = new int ; for (i = 0; i < N; i++) { shuzu[i] = (int) (100 + Math.random() * (100 + 1)); } System.out.print("顺序查找演示\n"); System.out.print("数据序列\n"); for (i = 0; i < N; i++) { System.out.print(" " + shuzu[i]); } System.out.print("\n\n"); System.out.print("输入要查找的数\n"); Scanner sc = new Scanner(System.in); x = sc.nextInt(); n = searchFun(shuzu, N, x); if (n < 0) { System.out.print("没找到数据" + x); } else { System.out.println("数据 " + x + "位于数据的第" + (n + 1) + "个元素处"); } } }
相关文章推荐
- 顺序查找算法
- 算法如功夫——二分查找和顺序查找的C代码
- 算法:顺序查找与折半查找
- JavaSE8基础 顺序查找优化算法(有哨兵)
- 一步一步复习数据结构和算法基础-顺序查找+二分查找
- 一步一步复习数据结构和算法基础-索引顺序表查找
- 算法—7.无序链表中的顺序查找
- 检索算法 ---顺序查找
- <sdut-ACM>数据结构实验之查找六:顺序查找
- 学习回顾算法(顺序查找算法)
- javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)
- 【数据结构作业一】写出顺序表的结构体类型定义及查找、插入、删除算法,并以顺序表作存储结构,实现线性表的插入、删除
- 查找算法之顺序搜索
- 算法3.1 顺序查找(基于无序链表)(algs4)
- 查找——线性表顺序查找算法
- 算法学习之查找(顺序、二分法、排序二叉树以及 Hash 表)
- java算法系列之(二)顺序查找算法
- 实现顺序查找的算法
- 【数据结构与算法】顺序查找
- Java查找算法之顺序查找