Detailed code for Java implementation of basic algorithm ideas
Algorithm is a program of the soul, a good algorithm can often be simplified, efficient solution problem. In the programming of the algorithm is independent of the language, regardless of which language can use these algorithms, the author will take the Java language as an example to introduce some common algorithm ideas.
Classification
- The idea of poor lifting algorithm
- The thought of recursive calculation method
- The idea of recursive algorithm
- The thought of divide and conquer algorithm
- The idea of probabilistic algorithm
The basic idea of poor lifting algorithm
Search for the right answer from all possible scenarios
1. For one possible situation, calculate the result.
2. Determine whether the result is satisfied, such as the failure of the person to perform the first step to search for the next possible situation, if satisfied is to choose to find a correct answer.
Example of an exhaustive algorithm
Only the chicken rabbit with the cage, on the 35 head, under 94 feet, ask the chicken rabbit geometry?
Algorithm implementation
int Qiongju(int head,int foot){ int i,j,chicken,rabbit; for(i=0;i<=head;i++) { j = head-i; if(i*2+j*4==foot) { chicken = i; rabbit = j; return 1; } } return 0;}
The basic idea of recursive method for thinking of recursive calculation method
Recursive method is a representative of rational thinking mode, which is gradually pushed to the result according to the existing data and relations. The process of execution of the Extrapolation method:
- Solve intermediate results based on known results and relationships
Determine if, but to require, if you do not continue with the first step, if there is a correct result is found
Recursive methods often need to know the actual logical relationship between the answer and the question. Many mathematical problems often have a definite formula to follow, so it can be implemented by recursive method.
Example of recursive methodFibonacci Sequence: Rabbit Litter problem
Title: If a pair of two-month-old rabbits can have a pair of rabbits every month, and the newborn rabbit can be born two months after the rabbit, assuming there is no rabbit death within a year, then a year after the number of rabbits?
Implementation algorithm
int Fibonacci(int n){ int t1,t2; if(n==1||n==2) { return 1; } else { t1 = Fibonacci(n-1); //递归调用 t2 = Fibonacci(n-2); return t1 + t2; }}
The idea of recursive algorithm
The recursive algorithm, which does not call itself repeatedly in the program to solve the problem, is a method that invokes its own method invocation in its method body. The main method in recursion is also the method of being modulated. Performing recursion will call itself repeatedly. Each call to a layer will enter a new layer.
Recursive invocation is divided into two situations:
- Direct recursion, that is, calling the method itself in the method
- Indirect recursion, that is, indirectly invoking a method
When you write a recursive method, you must use an if statement to force a return before recursion is performed.
Recursive algorithm examples
Recursive algorithms are often used for problems with obvious recursive properties and some mathematical problems.
Problem: Finding factorial
n!=n* (n-1) (n-2) (n-3) ... *2*1
(n-1)! = (n-1) (n-2) (n-3) ... *2*1
Get the recursive formula:
N! =n* (n-1)!
Algorithm implementation
long fact(int n){ if(n<=1) return 1; else return n*fact(n-1);}
The basic idea of divide-and-conquer algorithm thought
Divide-and-conquer algorithm is to divide a complex problem into smaller ones, calculate simple small problems, and then synthesize small problems to get the final answer. The procedure for splitting and administering the algorithm is as follows:
- For a size of n problem, if the problem is easier to solve, then directly resolved; otherwise, perform the following steps.
- The problem is decomposed into m-smaller problems, which are independent of each other and are independent from the original problem.
- Recursive these little problems
- Then, combine each small problem to get the solution of the problem
Examples of divide and conquer algorithms
Title: A bag has 30 coins, which has a counterfeit currency, and counterfeit money and real money a certain, the naked eye is difficult to distinguish, at present only know that counterfeit money is lighter than real money, how to distinguish counterfeit currency?
Algorithm analysis
- Start by numbering each coin, then divide all the coins into two parts and place them on either side of the balance.
- And then divide the lighter part into two copies. Repeat the above method
- Until two coins are left, the lighter one is counterfeit.
Algorithm implementation
int falsecoin (int coin[],int low,int high) {int i,sum1,sum2,sum3; int re = 0; sum1 = sum2 = sum3 = 0; if (Low+1==high) {if (Coin[low]<coin[high]) {re = low + 1; return re; } else {re = high +1; return re; }} if ((high-low+1)%2==0)//n is even {for (i=low;i<=low+ (high-low)/2;i++) {sum1= sum1+ Coin[i]; } for (i=low+low+ (high-low)/2;i<=high;i++) {sum2= sum2+coin[i]; } if (sum1>sum2) {Re=falsecoin (coin,low+ (high-low)/2,high); return re; } else if (sum1<sum2) {Re=falsecoin (coin,low,low+ (high-low)/2); return re; }} else {for (i=low;i<=low+ (high-low)/2-1;i++) {sum1= sum1+coin[i]; } for (i=low+low+ (high-low)/2+1;i<=high;i++) {sum2= sum2+coin[i]; } sum3=coin[low+ (High-Low)/2]; if (sum1>sum2) {Re=falsecoin (coin,low+ (high-low)/2+1,high); return re; } else if (sum1<sum2) {Re=falsecoin (coin,low,low+ (high-low)/2-1); return re; } else {re=low+ (high-low)/2+1; return re; }} return re;}
The basic algorithm thought Java implementation detailed code