本文目錄一覽:
用二分法查找(折半查找)java
二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。但是,折半查找要求線性表必須採用順序存儲結構,而且表中元素按關鍵字有序排列。
二分查找優缺點
優點是比較次數少,查找速度快,平均性能好;
其缺點是要求待查表為有序表,且插入刪除困難。
因此,折半查找方法適用於不經常變動而查找頻繁的有序列表。
使用條件:查找序列是順序結構,有序。
過程
首先,假設表中元素是按升序排列,將表中間位置記錄的關鍵字與查找關鍵字比較,如果兩者相等,則查找成功;否則利用中間位置記錄將表分成前、後兩個子表,如果中間位置記錄的關鍵字大於查找關鍵字,則進一步查找前一子表,否則進一步查找後一子表。重複以上過程,直到找到滿足條件的記錄,使查找成功,或直到子表不存在為止,此時查找不成功。
利用循環的方式實現二分法查找
public class BinarySearch {
public static void main(String[] args) {
// 生成一個隨機數組 int[] array = suiji();
// 對隨機數組排序 Arrays.sort(array);
System.out.println(“產生的隨機數組為: ” + Arrays.toString(array));
System.out.println(“要進行查找的值: “);
Scanner input = new Scanner(System.in);
// 進行查找的目標值 int aim = input.nextInt();
// 使用二分法查找 int index = binarySearch(array, aim);
System.out.println(“查找的值的索引位置: ” + index);
}
/** * 生成一個隨機數組 *
* @return 返回值,返回一個隨機數組 */
private static int[] suiji() {
// random.nextInt(n)+m 返回m到m+n-1之間的隨機數 int n = new Random().nextInt(6) + 5;
int[] array = new int[n];
// 循環遍歷為數組賦值 for (int i = 0; i array.length; i++) {
array[i] = new Random().nextInt(100);
}
return array;
}
/** * 二分法查找 —循環的方式實現 *
* @param array 要查找的數組 * @param aim 要查找的值 * @return 返回值,成功返回索引,失敗返回-1 */
private static int binarySearch(int[] array, int aim) {
// 數組最小索引值 int left = 0;
// 數組最大索引值 int right = array.length – 1;
int mid;
while (left = right) {
mid = (left + right) / 2;
// 若查找數值比中間值小,則以整個查找範圍的前半部分作為新的查找範圍 if (aim array[mid]) {
right = mid – 1;
// 若查找數值比中間值大,則以整個查找範圍的後半部分作為新的查找範圍 } else if (aim array[mid]) {
left = mid + 1;
// 若查找數據與中間元素值正好相等,則放回中間元素值的索引 } else {
return mid;
}
}
return -1;
}}
運行結果演示:
由以上運行結果我們得知,如果要查找的數據在數組中存在,則輸出該數據在數組中的索引;如果不存在則輸出 -1 ,也就是列印 -1 則該數在數組中不存在,反之則存在。
四、利用遞歸的方式實現二分法查找
public class BinarySearch2 {
public static void main(String[] args) {
// 生成一個隨機數組 int[] array = suiji();
// 對隨機數組排序 Arrays.sort(array);
System.out.println(“產生的隨機數組為: ” + Arrays.toString(array));
System.out.println(“要進行查找的值: “);
Scanner input = new Scanner(System.in);
// 進行查找的目標值 int aim = input.nextInt();
// 使用二分法查找 int index = binarySearch(array, aim, 0, array.length – 1);
System.out.println(“查找的值的索引位置: ” + index);
}
/** * 生成一個隨機數組 * * @return 返回值,返回一個隨機數組 */
private static int[] suiji() {
// Random.nextInt(n)+m 返回m到m+n-1之間的隨機數 int n = new Random().nextInt(6) + 5;
int[] array = new int[n];
// 循環遍歷為數組賦值 for (int i = 0; i array.length; i++) {
array[i] = new Random().nextInt(100);
}
return array;
}
/** * 二分法查找 —遞歸的方式 * * @param array 要查找的數組 * @param aim 要查找的值 * @param left 左邊最小值 * @param right 右邊最大值 * @return 返回值,成功返回索引,失敗返回-1 */
private static int binarySearch(int[] array, int aim, int left, int right) {
if (aim array[left] || aim array[right]) {
return -1;
}
// 找中間值 int mid = (left + right) / 2;
if (array[mid] == aim) {
return mid;
} else if (array[mid] aim) {
//如果中間值大於要找的值則從左邊一半繼續遞歸 return binarySearch(array, aim, left, mid – 1);
} else {
//如果中間值小於要找的值則從右邊一半繼續遞歸 return binarySearch(array, aim, mid + 1, array.length-1);
}
}}
運行結果演示:
總結:
遞歸相較於循環,代碼比較簡潔,但是時間和空間消耗比較大,效率低。在實際的學習與工作中,根據情況選擇使用。通常我們如果使用循環實現代碼只要不是太繁瑣都選擇循環的方式實現~
Java二分法
首先得告訴你,二分法的前提是必須是順序方式存儲,而且必須是排好序了的。比如要從100個數中查找某一個數,前提是這一百個數是排好序(這裡假如從小到大)的,然後找到最中間的數,若最中間的數(這裡是第50個)比你要找的這個數大那你只需要在1到49個數里找,然後再取最中間的數,再判斷,如此往複下去,最多次數,你算算看,
java 二分法 排序
二分排序就是用先用二分查找法來查某一個元素,然後再用別的排序演算法來進行排序。
package insert;
public class InsArrayApp {
public static void main(String[] args) {
int size = 100;
InsArray arr = new InsArray(size);
arr.insert(10);
arr.insert(9);
arr.insert(8);
arr.insert(7);
arr.insert(6);
arr.insert(10);
arr.insert(9);
arr.insert(8);
arr.insert(5);
arr.insert(4);
arr.insert(3);
arr.insert(2);
arr.insert(1);
arr.display();
// arr.insertSort();
// arr.display();
// System.out.println(arr.median());
// arr.noDups();
arr.noDups2();
arr.display();
}
}
class InsArray {
private int[] a;
private int nElems;
public InsArray(int size) {
a = new int[size];
nElems = 0;
}
public void insert(int value) {
a[nElems] = value;
nElems++;
}
public void display() {
for (int i = 0; i nElems; i++) {
System.out.print(a[i] + ” “);
}
System.out.println();
}
public void insertSort() {
int out, in;
int copy = 0;
int compare = 0;
/* for(out = 1;outnElems;out++){
int tmp = a[out];
in = out;
while(in0a[in-1]=tmp){
a[in] = a[in-1];
–in;
}
a[in] = tmp;
}*/
for(out = 1;outnElems;out++){
int tmp = a[out];
in = out;
while(in0){
if(a[in-1]=tmp){
a[in] = a[in-1];
–in;
++copy;
++compare;}
else{
break;
}
}
++compare;
a[in] = tmp;
}
System.out.println(“copy:” + copy + “compare:” + compare);
}
public int median(){
insertSort();
int m = nElems/2;
return a[m];
}
public void noDups(){
insertSort();
/*
InsArray tmp = new InsArray(nElems);
for(int i = 0;inElems;i++){
for(int j = i+1;jnElems;j++)
if(a[i] == a[j]){
a[j] = -1;
}
if(a[i]!=-1)
tmp.insert(a[i]);
}
*/
InsArray tmp = new InsArray(nElems);
int i;
for(int j = 0;jthis.nElems;j++){
/*if(tmp.nElems==tmp.find(this.a[j])) //binary find
tmp.insert(this.a[j]);
else
continue;*/
for( i = 0; i tmp.nElems; i++) { // for each element
if(tmp.a[i]==this.a[j]) // found searchKey?
break;
}
if(i==tmp.nElems) // no
tmp.insert(this.a[j]);
}
this.a = tmp.a;
this.nElems = tmp.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;
else if(lowerBoundupperBound)
return nElems;
else {
if(a[curIn]searchKey)
upperBound = curIn-1;
else
lowerBound = curIn+1;
}
}
}
public void noDups2(){
insertSort();
for(int i = 0;inElems;i++){
for(int j = i+1;jnElems;j++)
if(a[i] == a[j]){
a[j] = -1;
}
}
display();
int index = 0;
for(int i=0;inElems;i++){
if(a[i]!=-1){
index++;
}else{
for(int j=index+1;jnElems;j++){
if(a[j]!=-1){
a[index] = a[j];
a[j]=-1;
index++;
break;
}
}
}
}
nElems = index;
}
}
上面的代碼,是我以前敲的,有個find()方法是二分查找,然後再用插入排序去進行排序。
怎麼計算java二分法查找的比較次數
您好,我來為您解答:
演算法:當數據量很大適宜採用該方法。採用二分法查找時,數據需是有序不重複的。
基本思想:假設數據是按升序排序的,對於給定值
x,從序列的中間位置開始比較,如果當前位置值等於
x,則查找成功;若
x
小於當前位置值,則在數列的前半段中查找;若
x
大於當前位置值則在數列的後半段中繼續查找,直到找到為止。
希望我的回答對你有幫助。
java二分法查找重複數字的下標?
package pers.ly.javase.algorithm;import java.util.Arrays;/**
* 二分法查找
* @author: Lu Yang
* @date: 2019-01-23 10:50:37
*
*/public class BinarySearch {
public static void main(String[] args) {
Integer[] arr = {10,50,30,40,10,80,90,70,60,40,100,10};
// 數組排序 – 二分法必要條件
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println(binarySearch(arr,50));
}
/**
*
* @author: Lu Yang
* @date: 2019-01-23 11:44:01
* @param arr 數組
* @param value 數組元素值
* @return
*
*/
public static Integer binarySearch(Integer[] arr, Integer value) {
// 定義數組開始位置
Integer start = 0;
// 定義數組結束位置(arr.length是計算數組從1開始的總長度,arr.length-1計算數組從0開始的總長度)
Integer end = arr.length – 1;
// 開始位置 = 結束位置
while (start = end) {
// 定義數組的中心位置(開始位置+結束位置)/2
Integer mid = (start + end) / 2;
// 判斷數組mid位置值(當前數據中間位置值)是否小於傳過來的值
if (arr[mid] value)
// 如果小於傳過來的值,數組開始位置則定義中間位置下標+1
start = mid + 1;
// 判斷數組mid位置值(當前數據中間位置值)是否大於傳過來的值
if (arr[mid] value)
// 如果大於傳過來的值,數組結束位置則定義中間位置下標-1
end = mid – 1;
if (arr[mid] == value)
return mid;
}
return -1;
}}
二分法查找的java代碼
public class ef {
public static void main(String[] args) {
int[] a = { 4, 8, 12, 44, 69, 71, 98, 132 ,133};
int m = ef.ef(a, 0, a.length, 71);
if(m!=-1){
System.out.println(a[m]+”=====”+m);
}
System.out.println(“不存在該數字”);
}
public static int ef(int[] a, int from, int to, int b) {
int midel = (from + to) / 2;
if ((to – from) = 1 b != a[midel]) {
return -1;
}
if (b a[midel]) {
return ef(a, midel, to, b);
} else if (b a[midel]) {
return ef(a, from, midel, b);
} else {
return midel;
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239798.html