ECMAScript5 also added 2 methods for merging arrays: reduce () and reduceright ().
All the entries for both the iterated algebra groups
Reduce (): Iterate from the first entry to the end.
Reduceright (): Begins with the last item of the array, traversing to the first item of the array.
Both methods accept two parameters: the function called on each item (parameter: Previous value, current value, index of item, array object)
Any value returned by this function the fight is automatically passed to the next item as the first argument. The first iteration takes place on the second item of the array,
So the first argument is the first item in the array, and the second argument is the second item of the array.
and the initial value as the base of the merge.
Use the reduce () method to perform operations on the sum of all values in the array, such as:
Copy Code code as follows:
var values = [1, 2, 3, 4, 5];
var sum = values.reduce (function (prev, cur, index, array) {
return prev + cur;
});
alert (sum);
As it turns out, it's just the other way around.
var sum2=values.reduceright (function (Prev,cur,index,array) {
return prev+cur;
});
alert (SUM2);
Merge sort is an efficient sorting algorithm based on merging operations. The algorithm is a very typical application of the partition method (Divide and Conquer).
The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.
Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.