查找算法
查找算法是在存在的序列(list) 中查找特定的目标(target),要求序列中每个记录必须与一个关键词(key)关联才能进行查找。
![](https://img-my.csdn.net/uploads/201207/18/1342575855_3923.png)
查找算法通常需要两个输入:
1、被查找的序列 2、要查找的关键词
-----------------------------------------------------------------------------------------------------------------------------
顺序查找算法
顺序查找算法的思路很简单:
从表的第一个元素开始一个一个向下查找,如果有和目标一致的元素,查找成功;如果到最后一个元素仍没有目标元素,则查找失败。
package com.pjm.testjava.suanfa;
/**
* (1) java实现查找算法:线性查找
* 顺序查找也称为线形查找,从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字
* 与给定值k相比较,若相等则表示查找成功;若扫描结束仍没有找到关键字等于k的结点,表示查找失败。
*
* ★问题说明: 查找到Key时,打印Search Time时 永远是的输出 1。理解参数传递。
* 在Linear_Search方法中Counter的值确实改变了。但是返回到main方法中Counter的值仍未1。
*
* @author pan
*
*/
public class LSearch1 {
// ---------------------------------------------------
// 顺序查找(线性查找 从头到尾依次查找)
// ---------------------------------------------------
/**
* int[] Data 数组 int Key 待查找值 Counter 查找次数计数器 返回值 boolean
* 查找到返回true,未找到返回false。
*/
public static boolean Linear_Search(int[] Data, int Key, int Counter) {
int i; // 数据索引计数变量
for (i = 0; i < Data.length; i++) {
// 输出数据
System.out.print("[" + (int) Data[i] + "]");
// 查找到数据时
if ((int) Key == (int) Data[i])
return true; // 传回true
Counter++; // 计数器递增
}
return false; // 传回false
}
public static void main(String args[]) {
// 测试数据
int[] Data = { 12, 76, 29, 22, 15, 62, 29, 58, 35, 67, 58, 33, 28, 89,
90, 28, 64, 48, 20, 77 }; // 输入数据数组,长度20
int Counter = 1; // 查找次数计数变量
int KeyValue = 22;
// 调用线性查找
if (Linear_Search(Data, KeyValue, Counter)) {
// 输出查找次数
System.out.println("");
System.out.println("Search Time = " + Counter);
} else {
// 输出没有找到数据
System.out.println("");
System.out.println("No Found!!");
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
二分查找算法
二分查找前提是表是按递增或递减顺序的规范表。二分查找从表中间开始查找目标元素。如果找到一致元素,则查找成功。如果中间元素比目标元素小,则仍用二分查找方法查找表的后半部分(表是递增排列的),反之中间元素比目标元素大,则查找表的前半部分。
假设 数组中存在重复元素 那么折半查找 将返回 首次查到 数据的索引值 。
/**
* (2) java实现查找算法:折半查找(有前提 数组必须是 有序数组)
*
* 二分查找要求线形表中的结点按关键字值升序或降序排列,用给定值k先与中间结点的关键字比较,
* 中间结点把线形表分成两个子表,若相等则查找成功;若不相等,再根据k与该中间结点关键字的
* 比较结果确定下一步查找哪个子表,这样递归进行,直到查找到或查找结束发现表中没有这样的结点。
*/
public class BSearch2 {
// ---------------------------------------------------
// 折半查找法1 (无重复序列的查找)
// ---------------------------------------------------
/**
* int[] data 数据源
* int key 查找的值
*
* 返回值 int 查找到则返回 该值对应的索引值,如不存在则返回-1
*/
public static int binearySeach(int[] data, int key) {
int start = 0; //首次 起始index (0)
int end = data.length-1; //首次 尾部index (数组长度-1 索引)
int middle =0;
while(start<=end){
middle = (start+end)/2;//首次 中间index ((start+end)/2)
if(data[middle]==key){
//查找到则返回索引 (while循环的出口)
return middle;
}else if(data[middle]>key){
end = middle-1;
}else{
start = middle+1;
}
}
//满足start<=end的条件下未查找到 结束 while循环 返回-1
return -1;
}
public static void main(String[] args) {
//测试数据
int[] Data = { 12, 16, 19, 22, 25, 32, 39, 48, 55, 57, 58,
63, 68, 69, 70, 78, 84, 88, 90, 97 };
//查找kye为22,返回22对应的索引值index
int index = binearySeach(Data, 97);
System.out.println(index);
}
}
###################################################
java数据结构与算法一书中的例子 <find 二分查找,前提是 数组中不存在重复元素>
// orderedArray.java
// demonstrates ordered array class
// to run this program: C>java OrderedApp
class OrdArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
//-----------------------------------------------------------
public OrdArray(int max) // constructor
{
a = new long[max]; // create array
nElems = 0;
}
//-----------------------------------------------------------
public int size()
{ return nElems; }
//-----------------------------------------------------------
public int find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
//-----------------------------------------------------------
public void insert(long value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//-----------------------------------------------------------
public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//-----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-----------------------------------------------------------
} // end class OrdArray
class OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
int searchKey = 55; // search for item
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display(); // display items
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class OrderedApp