Programming algorithms and Algorithms

Source: Internet
Author: User

Programming algorithms and Algorithms
K-way merge sort code (C ++)


Address: http://blog.csdn.net/caroline_wendy


K-path Merge SortingAs a classic external sorting algorithm, programmers must master it.

Knowledge Concept reference: <Data Structure>


Main Ideas: In k sorted files, select the first value, use the loser tree, update the binary tree structure, and finally select the optimal value.

The code is for reference only. For example, the minimum value is replaced by (-1), and the maximum value is replaced by (100.


/** Main. cpp ** Created on: September 11, 2014 * Author: Spike */# include <fstream> # include <iostream> # include <vector> using namespace std; class KWayMergeSort {public: KWayMergeSort (vector <ifstream *> & vif): m_vifs (vif), m_ofs ("out.txt") {} void calculate () {/* the key words of the current first record of the segment read from k input merging segments to the external node */for (int I = 0; I <k; ++ I) {input (I, B [I]);} createLoserTree (); while (B [ls [0]! = MAXKEY) {/* q indicates the merging segment where the current minimum keyword is located */int q = ls [0]; /* merge the current segments numbered q (Keyword: B [q]. key) records written to the output merging segment */cout <B [q] <""; m_ofs <B [q] <""; /* read the keyword of the next record from the input merging segment numbered q */input (q, B [q]);/* adjust the loser tree, select the new minimum keyword */adjust (q);}/* write the record containing the maximum keyword MAXKEY to the output merging segment */cout <endl; m_ofs <endl ;} private: void input (int I, int & B) {if (* m_vifs [I]). good () {(* (m_vifs [I])> B ;}} /*** known B [0] to B [k-1] is the leaf node of the Complete Binary Tree ls, there are k keywords, along the slave leaf The k-path from the child * to the root will adjust the ls to become the loser tree. */Void createLoserTree () {B [k] = MINKEY;/* set the initial value of "loser" in ls */for (int I = 0; I <k; + + I) {ls [I] = k;}/* sequentially from B [k-1], B [K-2],…, B [0] Starting to adjust the loser */for (int I = k-1; I> = 0; -- I) {adjust (I) ;}} void adjust (int I) {/* ls [t] is the parent node of B [s] */int t = (I + k)/2; while (t> 0) {/* s indicates the new winner */if (B [I]> B [ls [t]) {int tmp = I; I = ls [t]; ls [t] = tmp;} t = t/2;} ls [0] = I;} private: static const int k = 4; static const int MINKEY =-1; static const int MAXKEY = 100; int B [k + 1]; // The leaf node int ls [k] of the loser tree; // vector <ifstream *> m_vifs; ofstream m_ofs ;}; int main (void) {vector <ifstream *> vif; ifstream * ifs0 = new ifstream ("f0.txt"); ifstream * ifs1 = new ifstream ("f1.txt"); ifstream * ifs2 = new ifstream ("f2.txt "); ifstream * ifs3 = new ifstream ("f3.txt"); vif. push_back (ifs0); vif. push_back (ifs1); vif. push_back (ifs2); vif. push_back (ifs3); KWayMergeSort k (vif); k. calculate (); delete ifs0; delete ifs1; delete ifs2; delete ifs3; return 0 ;}

Files to be sorted, The last one is tag, used Stop updateLoser tree:

f0: 5 16 49 52 78 100f1: 7 12 25 84 91 100f2: 29 38 57 66 71 100f3: 9 22 47 48 59 100

Output:

5 7 9 12 16 22 25 29 38 47 48 49 52 57 59 66 71 78 84 91 







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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.