算法知識梳理(8) 二分查找算法及其變型

1、概要

本文介紹了有關二分查找的算法的Java代碼實現,全部代碼都可經過 在線編譯器 直接運行,算法目錄:java

  • 普通二分查找
  • 查找關鍵字第一次出現的位置
  • 查找關鍵字最後一次出現的位置
  • 查找小於關鍵字的最大數字出現的位置
  • 查找大於關鍵字的最小數字出現的位置

對於二分查找,有如下幾個須要注意的點:面試

  • pStartpEnd修改的條件進行合併.
  • 若是出現了pEnd=pMid的狀況要將while條件修改成pStart<pEnd
  • 若是出現了pStart=pMid的狀況要將while條件修改成pStart<pEnd-1
  • 對於pStart<pEnd的結束條件,最後一次進入循環的數組長度爲2,結束時數組長度爲1
  • 對於pStart<pEnd-1的結束條件,最後一次進入循環的數組長度爲3,結束時數組長度爲12

2、代碼實現

2.1 普通二分查找

實現代碼

class Untitled {
	
	static int binNormalSearch(int p[], int len, int key) {
		int start = 0;
		int end = len-1;
		while (start <= end) {
			int mid = (start+end) >> 1;
			if (p[mid] > key) {
				end = mid-1;
			} else if (p[mid] < key) {
				start = mid+1;
			} else {
				return mid;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		int p[] = {1, 5, 6, 7, 11, 11, 56};
		System.out.println("index=" + binNormalSearch(p, p.length, 11));
	}
}
複製代碼

2.2 查找關鍵字第一次出現的位置

實現代碼

class Untitled {

	static int binFirstKSearch(int p[], int len, int key) {
		int start = 0;
		int end = len-1;
		while (start < end) {
			int mid = (start+end) >> 1;
			if (p[mid] > key) {
				end = mid-1;
			} else if (p[mid] < key) {
				start = mid+1;
			} else {
				end = mid;
			}
		}
		if (p[start] == key) {
			return start;
		}
		return -1;
	}

	public static void main(String[] args) {
		int p[] = {1, 5, 6, 7, 11, 11, 11, 11, 56};
		System.out.println("index=" + binFirstKSearch(p, p.length, 11));
	}
}
複製代碼

2.3 查找關鍵字最後一次出現的位置

實現代碼

class Untitled {

	static int binLastKSearch(int p[], int len, int key) {
		int start = 0;
		int end = len-1;
		while (start < end-1) {
			int mid = (start+end) >> 1;
			if (p[mid] > key) {
				end = mid-1;
			} else if (p[mid] < key) {
				start = mid+1;
			} else {
				start = mid;
			}
		}
		if (p[end] == key) {
			return end;
		}
		if (p[start] == key) {
			return start;
		}
		return -1;
	}

	public static void main(String[] args) {
		int p[] = {1, 5, 6, 7, 11, 11, 11, 11, 56};
		System.out.println("index=" + binLastKSearch(p, p.length, 11));
	}
}
複製代碼

2.4 查找小於關鍵字的最大數字出現的位置

代碼實現

class Untitled {

	static int binMaxLessKSearch(int p[], int len, int key) {
		int start = 0;
		int end = len-1;
		while (start < end-1) {
			int mid = (start+end) >> 1;
			if (p[mid] > key) {
				end = mid-1;
			} else if (p[mid] < key) {
				start = mid;
			} else {
				end = mid-1;
			}
		}
		if (p[end] < key) {
			return end;
		}
		if (p[start] < key) {
			return start;
		}
		return -1;
	}

	public static void main(String[] args) {
		int p[] = {1, 5, 6, 7, 11, 11, 11, 11, 56};
		System.out.println("index=" + binMaxLessKSearch(p, p.length, 11));
	}
}
複製代碼

2.5 查找大於關鍵字的最小數字出現的位置

代碼實現

class Untitled {

	static int binMinMoreKSearch(int p[], int len, int key) {
		int start = 0;
		int end = len-1;
		while (start < end) {
			int mid = (start+end) >> 1;
			if (p[mid] > key) {
				end = mid;
			} else if (p[mid] < key) {
				start = mid+1;
			} else {
				start = mid+1;
			}
		}
		if (p[start] > key) {
			return start;
		}
		return -1;
	}

	public static void main(String[] args) {
		int p[] = {1, 5, 6, 7, 11, 11, 11, 11, 56};
		System.out.println("index=" + binMinMoreKSearch(p, p.length, 11));
	}
}
複製代碼

更多文章,歡迎訪問個人 Android 知識梳理系列:

相關文章
相關標籤/搜索