__NOTOC__ Merge algorithms are a family of algorithms that run sequentially over multiple sorted lists, typically producing more sorted lists as output. This is well-suited for machines with tape drives. Use has declined due to large random access memories, and many applications of merge algorithms have faster alternatives when you have a random-access memory that holds all your data.
The general merge algorithm has a set of pointers p0..n that point to positions in a set of lists L0..n. Initially they point to the first item in each list. The algorithm is as follows:
While any of p0..n still point to data inside of L0..n instead of past the end:
function merge(a, b) var list result var int i, j := 0 if length(a) = 0 return b if length(b) = 0 return a while (i < length(a)) and (j < length(b)) if a<= b[j add a* to result if a= b[j j := j + 1 i := i + 1 else add b* to result j := j + 1 while i < length(a) add a* to result i := i + 1 while j < length(b) add b* to result j := j + 1 return result
The classic merge (the one used in merge sort) outputs the data item with the lowest key at each step; given some sorted lists, it produces a sorted list containing all the elements in any of the input lists, and it does so in time proportional to the sum of the lengths of the input lists.
void Merge(float v*, int start, int mid, int end) { int i, j, k; float* tmp = malloc(sizeof(float) * (end - start)); i = start; j = mid; k = 0; while ((i < mid) && (j < end)) { if (v<= v[j) tmp= v[i++; else tmp= v[j++; } while (i < mid) tmp= v[i++; while (j < end) tmp= v[j++; for (i = 0; i < (end-start); i++) v= tmp[i; free(tmp); }
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Merge algorithm".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world