算法(二):查找
2016-03-31 15:09
295 查看
一 、 线性查找(顺序查找)
public class LSearch {
public static int[] Data = { 12, 76, 29, 22, 15, 62, 29, 58, 35, 67,
58, 33, 28, 89, 90, 28, 64, 48, 20, 77 }; // 输入数据数组
public static int Counter = 1; // 查找次数计数变量
public static void main(String args[]) {
int KeyValue = 22;
// 调用线性查找
if (Linear_Search((int) KeyValue)) {
// 输出查找次数
System.out.println("");
System.out.println("Search Time = " + (int) Counter);
} else {
// 输出没有找到数据
System.out.println("");
System.out.println("No Found!!");
}
}
// ---------------------------------------------------
// 顺序查找
// ---------------------------------------------------
public static boolean Linear_Search(int Key) {
int i; // 数据索引计数变量
for (i = 0; i < 20; i++) {
// 输出数据
System.out.print("[" + (int) Data[i] + "]");
// 查找到数据时
if ((int) Key == (int) Data[i])
return true; // 传回true
Counter++; // 计数器递增
}
return false; // 传回false
}
}
二 、[b]二分查找(折半查找)[/b]
public class BSearch {
public static int Max = 20;
public static int[] Data = { 12, 16, 19, 22, 25, 32, 39, 48, 55, 57,
58, 63, 68, 69, 70, 78, 84, 88, 90, 97 }; // 数据数组
public static int Counter = 1; // 计数器
public static void main(String args[]) {
int KeyValue = 22;
// 调用折半查找
if (BinarySearch((int) KeyValue)) {
// 输出查找次数
System.out.println("");
System.out.println("Search Time = " + (int) Counter);
} else {
// 输出没有找到数据
System.out.println("");
System.out.println("No Found!!");
}
}
// ---------------------------------------------------
// 折半查找法
// ---------------------------------------------------
public static boolean BinarySearch(int KeyValue) {
int Left; // 左边界变量
int Right; // 右边界变量
int Middle; // 中位数变量
Left = 0;
Right = Max - 1;
while (Left <= Right) {
Middle = (Left + Right) / 2;
if (KeyValue < Data[Middle]) // 欲查找值较小
Right = Middle - 1; // 查找前半段
// 欲查找值较大
else if (KeyValue > Data[Middle])
Left = Middle + 1; // 查找后半段
// 查找到数据
else if (KeyValue == Data[Middle]) {
System.out
.println("Data[" + Middle + "] = " + Data[Middle]);
return true;
}
Counter++;
}
return false;
}
}
public class LSearch {
public static int[] Data = { 12, 76, 29, 22, 15, 62, 29, 58, 35, 67,
58, 33, 28, 89, 90, 28, 64, 48, 20, 77 }; // 输入数据数组
public static int Counter = 1; // 查找次数计数变量
public static void main(String args[]) {
int KeyValue = 22;
// 调用线性查找
if (Linear_Search((int) KeyValue)) {
// 输出查找次数
System.out.println("");
System.out.println("Search Time = " + (int) Counter);
} else {
// 输出没有找到数据
System.out.println("");
System.out.println("No Found!!");
}
}
// ---------------------------------------------------
// 顺序查找
// ---------------------------------------------------
public static boolean Linear_Search(int Key) {
int i; // 数据索引计数变量
for (i = 0; i < 20; i++) {
// 输出数据
System.out.print("[" + (int) Data[i] + "]");
// 查找到数据时
if ((int) Key == (int) Data[i])
return true; // 传回true
Counter++; // 计数器递增
}
return false; // 传回false
}
}
二 、[b]二分查找(折半查找)[/b]
public class BSearch {
public static int Max = 20;
public static int[] Data = { 12, 16, 19, 22, 25, 32, 39, 48, 55, 57,
58, 63, 68, 69, 70, 78, 84, 88, 90, 97 }; // 数据数组
public static int Counter = 1; // 计数器
public static void main(String args[]) {
int KeyValue = 22;
// 调用折半查找
if (BinarySearch((int) KeyValue)) {
// 输出查找次数
System.out.println("");
System.out.println("Search Time = " + (int) Counter);
} else {
// 输出没有找到数据
System.out.println("");
System.out.println("No Found!!");
}
}
// ---------------------------------------------------
// 折半查找法
// ---------------------------------------------------
public static boolean BinarySearch(int KeyValue) {
int Left; // 左边界变量
int Right; // 右边界变量
int Middle; // 中位数变量
Left = 0;
Right = Max - 1;
while (Left <= Right) {
Middle = (Left + Right) / 2;
if (KeyValue < Data[Middle]) // 欲查找值较小
Right = Middle - 1; // 查找前半段
// 欲查找值较大
else if (KeyValue > Data[Middle])
Left = Middle + 1; // 查找后半段
// 查找到数据
else if (KeyValue == Data[Middle]) {
System.out
.println("Data[" + Middle + "] = " + Data[Middle]);
return true;
}
Counter++;
}
return false;
}
}
相关文章推荐
- Android Studio运行时finished with non-zero exit value 2或:app:transformClassesWithDexForDebug
- Ruby中Time对象的常用函数总结
- android之TabLayout实现PagerSlidingTabStrip,ViewpagerIndicator之类的效果滑动页面导航效果(类似网易新闻)
- Codeforces 366D Dima and Trap Graph 【并查集】
- 简单常用滤波算法C语言实现
- PNP型三极管与NPN型三极管区别
- delete,truncate和drop的区别
- (第6讲)jdk,jre,JVM
- adb shell 获得活动窗口信息
- Tuning-java-garbage-collection-for-spark-applications
- 导航栏透明以及tableview起点不能从navgationBar顶部开始问题
- CWNP宣布中国首位CWNE获得者——朱志立(Kevin Zhu)
- MySQL数据类型之CHAR与VARCHAR及row size max=65535bytes
- jQuery -- 操作select
- Java 变量初始化时机
- 细说业务逻辑(待读)
- spring定时任务轮询(spring Task)
- C++中的虚函数
- iOS -- 请求网络数据&数据处理
- MyBatis 查询