Introductory tutorial on js divide and conquer method, solution to binary search

Insert image description here

1. Definition of divide and conquer law

In computer science, divide and conquer is a very important algorithm. The literal explanation is "divide and conquer". The divide and conquer method is to divide a complex problem into two or more identical or similar sub-problems, and then divide the sub-problems into smaller sub-problems... until the sub-problems can be simplified in the end. The direct solution of , the solution of the original problem is the combination of the solutions of the sub-problems.

  1. The essence of the divide and conquer method can also be said to be the steps:
    • Points: Decomposition - decompose the original problem into smaller, independent sub-problems with the same form as the original problem;
    • Treatment: Solve - if the sub-problem is small and easy to be solved, solve it directly, otherwise solve each sub-problem recursively;
    • Combined: Merge - combine the solutions of each sub-problem into the solution of the original problem.
  2. The problems that can be solved by the divide and conquer method generally have the followingcharacteristics:
    • The problem can be easily solved if it is reduced in size to a certain extent;
    • This problem can be decomposed into several smaller identical problems, that is, the problem has optimal substructure properties; (the premise of applying the divide-and-conquer method, this feature reflects Recursionthought)
    • The solutions to the sub-problems decomposed using this problem can be merged into the solution of this problem; (key point, if this feature is not available, greedy algorithm or dynamic programming can be considered)
    • The sub-problems decomposed from this problem are independent of each other, that is, there are no common sub-problems between the sub-problems. (Do not include common sub-problems, that is, the sub-problems are not repeated. If they are repeated, dynamic programming should be considered)

Next, we usebinary search as an example.

2. What is Binary Search?

Binary search method, which makes full use of the order relationship between elements and adopts the divide-and-conquer strategy, can complete the search task in O(log n) in the worst case.

1. Prerequisites for using binary search method:

  • The array must be an ordered array, such as [1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Principle of binary search method:

  1. The search starts from the middle element of the array. If the middle element happens to be the element to be found, the search process ends;
  2. If the middle element is larger than the element you are looking for, continue searching in half the range that is smaller than the middle element in the array;
  3. If the middle element is smaller than the element to be found, continue searching in half of the range that is larger than the middle element in the array;
  4. Repeat steps 2 and 3 until the element is found. If the element is not found when there is no remaining interval, the element does not exist.

When using the binary search method to find elements, half of the interval can be eliminated every time, so the time complexity is O(log n) logarithmic order.

3. Code analysis:

  • First, let’s analyze the variables needed in the code; because it is a range movement of an interval, a head and a tail are needed to determine an interval; the variables that define the head and tail are head and tail; because each search needs to reduce half of the interval, an intermediate element is required to be middle and needs to be moved each time head and tail to determine the remaining half of the interval; finally, a search target element target is needed;
  • Boundary conditions are found: the interval is empty. So when is the interval empty? It must be when head > tail;
  • Repeat the steps of reducing the interval by half until the target element is found. If the target element is not found, -1 is returned.

Define binary search method binarySearch:

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function binarySearch(target){
    
    
	let head = 0; // 初始化头部索引
	let tail = arr.length - 1; // 初始化尾部索引
	while( head <= tail){
    
    
		let middle = Math.floor((head + tail) / 2); // 每次查找计算中间元素的索引
		if (target === arr[middle]){
    
     // 找到目标元素,返回目标元素的索引
			return middle;
		} else if (target < arr[middle]){
    
    
			tail = middle - 1; // 如果目标元素小于中间元素,则移动尾部索引位置为 middle - 1
		} else if (target > arr[middle]){
    
    
			head = middle + 1; // 如果目标元素大于中间元素,则移动头部索引位置为 middle + 1
		}
	}
	return -1; // 未找到目标元素,返回-1
}
let result = binarySearch(8);
console.log(result);  // 7
let result2 = binarySearch(9);
console.log(result2);  // 8
let result3 = binarySearch(0);
console.log(result3);  // -1

Insert image description here

Some classic problems that can also be solved using the divide-and-conquer algorithm:

  • binary search
  • Large integer multiplication
  • chessboard overlay
  • merge sort
  • Quick sort
  • linear time selection
  • closest point pair problem
  • Round robin schedule
  • Tower of Hanoi

Guess you like

Origin blog.csdn.net/ThisEqualThis/article/details/129687207