Programming algorithms and Algorithms
Quick sorting and BinarySearch)
Address: http://blog.csdn.net/caroline_wendy
Quick sorting and Binary SearchThis article mainly explains how to write these two classical algorithms.
The two algorithms that programmers must master are required to use paper in any language.
Fast sorting (C ):
/** Main. cpp ** Created on: September 10, 2014 * Author: Spike */# include <stdio. h> # include <stdlib. h >#include <iostream >#include <exception> int RandomInRange (int start, int end) {int res = rand () % (end-start + 1) + start; return res;} void Swap (int * num1, int * num2) {int tmp = * num1; * num1 = * num2; * num2 = tmp ;} int Partition (int data [], int length, int start, int end) {if (data = NULL | length <= 0 | start> End | start <0 | end> = length) {return-1;} int index = RandomInRange (start, end); Swap (& data [index], & data [end]); int small = start-1; for (index = start; index <end; ++ index) {if (data [index] <data [end]) {small ++; if (small! = Index) Swap (& data [index], & data [small]) ;}} small ++; Swap (& data [small], & data [end]); return small;} void QuickSort (int data [], int length, int start, int end) {if (start = end) return; int index = Partition (data, length, start, end); if (index> start) QuickSort (data, length, start, index-1); if (index <end) QuickSort (data, length, index + 1, end) ;}int main (void) {int data [] = {1, 4, 5, 2, 3, 7, 8, 6}; int length = 8; quickSort (data, length, 0, length-1); for (int I = 0; I <length; ++ I) {printf ("% d ", data [I]);} printf ("\ n"); return 0 ;}
Binary Search (C ):
/** Main. cpp ** Created on: September 10, 2014 * Author: Spike */# include <stdio. h> # include <stdlib. h >#include <iostream >#include <exception> int BinarySearch (int data [], int length, int value) {int left = 0; int right = length-1; while (left <= right) {int middle = left + (right-left)> 1); if (data [middle]> value) right = middle-1; else if (data [middle] <value) left = middle + 1; elsereturn middle;} return-1;} int main (void) {int data [] = {1, 2, 3, 4, 5, 6, 7, 8}; int length = 8; printf ("% d", BinarySearch (data, length, 5 )); printf ("\ n"); return 0 ;}
Programming Algorithm Problems
Typical problems with Dynamic Planning of backpacks
1 search method: exhaustive query of all possible fetch Conditions
2. Dynamic Planning
Are you a newbie? Because the data size is very small, you can simply use the search method. It is easier to write programs because it is easier to understand the data ~~
You can find out about your backpack and provide detailed answers everywhere ~~~
Zhidao.baidu.com/question/25002746.html
Similar to this problem, you only need to change the question and code.
After the change, the idea is as follows to help you replace it with the C language ~~~~
There is a box with a capacity of v (positive integer, o ≤ v ≤ 20000) and n items (o ≤ n ≤ 30). Each item has a volume (positive integer ). From n items, if thousands of items are loaded into the box, the remaining space of the box must be zero. (Here v is your s, and here n is your n)
L search method
Void search (int k, int v) {searches for the k-th item, and the remaining space is v}
{
Int I, j;
If (v <best) best = v;
If v-(s [n]-s [k-1])> = best return; // s [n] indicates the weight and
If (k <= n ){
If (v> w [k]) search (k + 1, v-w [k]); // w [n] indicates the weight of the nth item
Search (k + 1, v );
}
}
Best is the global variable, indicating the minimum value of the remaining space of the box. It is good to set the initial value to a very large positive number.
Therefore, if the best value is 0 after search (n, v), it indicates that there is a solution.
2 DP Dynamic Planning (iterative method)
F [I, j] is a boolean type that selects a number of signs from the first I items to make the size exactly j.
Implementation: converts optimization problems into judgment problems
F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]) (w [I] <= j <= v) Boundary: f [0] [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]);
After calculation, if F [n] [v] is true, there is a solution.
3 DP Dynamic Planning (Iterative Optimization)
Optimization: the current status is only related to the status of the previous stage and can be reduced to one dimension.
F [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [j] = (F [j-w [I] | F [j]);
After calculation, if F [v] is true, there is a solution.
4 DP Dynamic Planning (memorandum algorithm)
F is a two-dimensional array. int F [n] [v] is initialized to 0 at the beginning.
Bool dp (int n, int v)
{
If (n = 0 & v = 0) return 1;
If (n = 0 & v! = 0) return-1;
If (F [n] [v]! = 0) return F [n] [v];
If (dp (n-1, v-w [n]) return F [n] [v] = 1;
If (v> = w [n] & dp (n-1, v-w [n]) return F [n] [v] = 1;
Return F [n] [v] =-1;
} ...... Remaining full text>
What does "algorithm" mean in programming?
For example, from B to a, there are many paths that can be taken, and any path can be considered as an algorithm.
There are also many different methods to solve a problem in programming. Each method is an algorithm.
There is always one of the best and most efficient algorithms. Whether or not the most efficient method can be used to complete the task is one of the performance of a programmer.