Divide and conquer algorithm of algorithm collection and its application algorithm

Divide and conquer

Self-understanding: It is to divide a big problem into small problems. The thinking is very similar to recursion. Divide and conquer, that is, divide a problem into several small problems, and finally summarize the solutions, and many problems are solved by recursion, for example Binary search (binary search )

Classic problems :
(1) Binary search
(2) Palindrome algorithm
(2) Large integer multiplication
(3) Strassen matrix multiplication
(4) Checkerboard cover
(5) Merge sort
(6) Quick sort
(7) Linear time selection
(8) The closest point to the problem
(9) Round-robin schedule
(10) Tower of Hanoi

(1) Binary search

Remember to pay attention to the end point of the recursion and the condition is low<=high, if there is no = sign, a value will be missed!
Time complexity: O (logn)

#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<Windows.h>
#include<math.h>
using namespace std;
int* n;   //时间复杂度:O(logn)

int bisearch(int m,int low,int high)
{
    
    
	if (low <= high)
	{
    
    
		int mid = (low + high) / 2;
		if (n[mid] == m)
			return 1;
		else if (n[mid] > m)
		{
    
    
			return bisearch(m, low, mid - 1);
		}
		else if (n[mid] < m)
		{
    
    
			return bisearch(m, mid + 1, high);
		}
	}
	else
	{
    
    
		return 0;
	}
}

Palindrome

int  palindrome(string str, int m, int n)
{
    
    
	if (m > n)
	{
    
    
		return 1;
	}
	else
	{
    
    
		return (str[m] == str[n]) && palindrome(str, m + 1, n - 1);
	}
}

Guess you like

Origin blog.csdn.net/qq_43978754/article/details/115051304