Javascript merge method and javascript merge
ECMAScript5 also adds two methods to merge Arrays: reduce () and reduceRight ().
Both of them iterate all the items in the array.
Reduce (): traverse from the first item to the end one by one.
ReduceRight (): traverses the first entry of the array from the last entry of the array.
Both methods accept two parameters: the function called on each item (the parameter is the previous value, the current value, the index of the item, and the array object)
Any value returned by this function will be automatically passed to the next item as the first parameter. The first iteration occurs in the second entry of the array,
Therefore, the first parameter is the first item of the array, and the second parameter is the second item of the array.
And the initial value as the basis for merging.
The reduce () method can be used to perform the operation of the sum of all values in the array, for example:
Copy codeThe Code is as follows:
Var values = [1, 2, 3, 4, 5];
Var sum = values. reduce (function (prev, cur, index, array ){
Return prev + cur;
});
Alert (sum );
// The result is the same, but the direction is the opposite.
Var sum2 = values. reduceRight (function (prev, cur, index, array ){
Return prev + cur;
});
Alert (sum2 );
Merge sort is an effective Sorting Algorithm Based on the Merge operation. This algorithm is a very typical application of Divide and Conquer.
The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.
Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of Divide and Conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge.