<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Merge_algorithm</id>
	<title>Merge algorithm - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Merge_algorithm"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;action=history"/>
	<updated>2026-05-01T14:46:21Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=3127205&amp;oldid=prev</id>
		<title>imported&gt;GhostInTheMachine: cleaner frameboxes</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=3127205&amp;oldid=prev"/>
		<updated>2025-11-11T20:32:51Z</updated>

		<summary type="html">&lt;p&gt;cleaner frameboxes&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Previous revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 20:32, 11 November 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l50&quot;&gt;Line 50:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 50:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Several solutions to this problem exist. A naive solution is to do a loop over the {{mvar|k}} lists to pick off the minimum element each time, and repeat this loop until all lists are empty:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Several solutions to this problem exist. A naive solution is to do a loop over the {{mvar|k}} lists to pick off the minimum element each time, and repeat this loop until all lists are empty:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;div style=&quot;&lt;/del&gt;margin-left&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;35px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;; &lt;/del&gt;width&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;600px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&quot;&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue|&lt;/ins&gt;margin-left&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;35px&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|&lt;/ins&gt;width&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;600px}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue&lt;/del&gt;}}&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Input: a list of {{mvar|k}} lists.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Input: a list of {{mvar|k}} lists.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* While any of the lists is non-empty:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* While any of the lists is non-empty:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l57&quot;&gt;Line 57:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 56:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;** Output the minimum element and remove it from its list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;** Output the minimum element and remove it from its list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Best, worst and average case|In the worst case]], this algorithm performs {{math|(&amp;#039;&amp;#039;k&amp;#039;&amp;#039;−1)(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;−{{sfrac|&amp;#039;&amp;#039;k&amp;#039;&amp;#039;|2}})}} element comparisons to perform its work if there are a total of {{mvar|n}} elements in the lists.&amp;lt;ref name=&amp;quot;greene&amp;quot;&amp;gt;{{cite conference |last=Greene |first=William A. |year=1993 |title=k-way Merging and k-ary Sorts |conference=Proc. 31-st Annual ACM Southeast Conf |pages=127–135 |url=http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf}}&amp;lt;/ref&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Best, worst and average case|In the worst case]], this algorithm performs {{math|(&amp;#039;&amp;#039;k&amp;#039;&amp;#039;−1)(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;−{{sfrac|&amp;#039;&amp;#039;k&amp;#039;&amp;#039;|2}})}} element comparisons to perform its work if there are a total of {{mvar|n}} elements in the lists.&amp;lt;ref name=&amp;quot;greene&amp;quot;&amp;gt;{{cite conference |last=Greene |first=William A. |year=1993 |title=k-way Merging and k-ary Sorts |conference=Proc. 31-st Annual ACM Southeast Conf |pages=127–135 |url=http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf}}&amp;lt;/ref&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It can be improved by storing the lists in a [[priority queue]] ([[heap (data structure)|min-heap]]) keyed by their first element:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It can be improved by storing the lists in a [[priority queue]] ([[heap (data structure)|min-heap]]) keyed by their first element:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;div style=&quot;&lt;/del&gt;margin-left&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;35px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;; &lt;/del&gt;width&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;600px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&quot;&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue|&lt;/ins&gt;margin-left&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;35px&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|&lt;/ins&gt;width&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;600px}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue&lt;/del&gt;}}&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Build a min-heap {{mvar|h}} of the {{mvar|k}} lists, using the first element as the key.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Build a min-heap {{mvar|h}} of the {{mvar|k}} lists, using the first element as the key.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* While any of the lists is non-empty:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* While any of the lists is non-empty:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l70&quot;&gt;Line 70:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 67:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;** Re-heapify {{mvar|h}}.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;** Re-heapify {{mvar|h}}.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Searching for the next smallest element to be output (find-min) and restoring heap order can now be done in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (more specifically, {{math|2⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons{{r|greene}}), and the full problem can be solved in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039; log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (approximately {{math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons).{{r|greene}}&amp;lt;ref name=&amp;quot;toolbox&amp;quot;&amp;gt;{{cite book|author1=Kurt Mehlhorn|author-link=Kurt Mehlhorn|author2=Peter Sanders|author2-link=Peter Sanders (computer scientist)|title=Algorithms and Data Structures: The Basic Toolbox |date=2008 |publisher=Springer |isbn=978-3-540-77978-0 |url=http://people.mpi-inf.mpg.de/~mehlhorn/ftp/Toolbox/}}&amp;lt;/ref&amp;gt;{{rp|119–120}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Searching for the next smallest element to be output (find-min) and restoring heap order can now be done in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (more specifically, {{math|2⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons{{r|greene}}), and the full problem can be solved in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039; log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (approximately {{math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons).{{r|greene}}&amp;lt;ref name=&amp;quot;toolbox&amp;quot;&amp;gt;{{cite book|author1=Kurt Mehlhorn|author-link=Kurt Mehlhorn|author2=Peter Sanders|author2-link=Peter Sanders (computer scientist)|title=Algorithms and Data Structures: The Basic Toolbox |date=2008 |publisher=Springer |isbn=978-3-540-77978-0 |url=http://people.mpi-inf.mpg.de/~mehlhorn/ftp/Toolbox/}}&amp;lt;/ref&amp;gt;{{rp|119–120}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l76&quot;&gt;Line 76:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 72:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A third algorithm for the problem is a [[divide and conquer algorithm|divide and conquer]] solution that builds on the binary merge algorithm:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A third algorithm for the problem is a [[divide and conquer algorithm|divide and conquer]] solution that builds on the binary merge algorithm:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;div style=&quot;&lt;/del&gt;margin-left&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;35px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;; &lt;/del&gt;width&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &lt;/del&gt;600px&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&quot;&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue|&lt;/ins&gt;margin-left&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;35px&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|&lt;/ins&gt;width&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/ins&gt;600px}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{framebox|blue&lt;/del&gt;}}&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 1}}, output the single input list.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 1}}, output the single input list.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 2}}, perform a binary merge.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 2}}, perform a binary merge.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Else, recursively merge the first {{math|⌊&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌋}} lists and the final {{math|⌈&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌉}} lists, then binary merge these.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Else, recursively merge the first {{math|⌊&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌋}} lists and the final {{math|⌈&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌉}} lists, then binary merge these.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;{{frame-footer}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;When the input lists to this algorithm are ordered by length, shortest first, it requires fewer than {{math|&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌈log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌉}} comparisons, i.e., less than half the number used by the heap-based algorithm; in practice, it may be about as fast or slow as the heap-based algorithm.{{r|greene}}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;When the input lists to this algorithm are ordered by length, shortest first, it requires fewer than {{math|&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌈log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌉}} comparisons, i.e., less than half the number used by the heap-based algorithm; in practice, it may be about as fast or slow as the heap-based algorithm.{{r|greene}}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>imported&gt;GhostInTheMachine</name></author>
	</entry>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=623993&amp;oldid=prev</id>
		<title>imported&gt;OAbot: Open access bot: arxiv updated in citation with #oabot.</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=623993&amp;oldid=prev"/>
		<updated>2025-06-18T18:45:29Z</updated>

		<summary type="html">&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/OABOT&quot; class=&quot;extiw&quot; title=&quot;wikipedia:OABOT&quot;&gt;Open access bot&lt;/a&gt;: arxiv updated in citation with #oabot.&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Previous revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 18:45, 18 June 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l126&quot;&gt;Line 126:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 126:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;There are also algorithms that introduce parallelism within a single instance of merging of two sorted lists. These can be used in field-programmable gate arrays ([[FPGA]]s), specialized sorting circuits, as well as in modern processors with single-instruction multiple-data ([[SIMD]]) instructions.  &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;There are also algorithms that introduce parallelism within a single instance of merging of two sorted lists. These can be used in field-programmable gate arrays ([[FPGA]]s), specialized sorting circuits, as well as in modern processors with single-instruction multiple-data ([[SIMD]]) instructions.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Existing parallel algorithms are based on modifications of the merge part of either the [[bitonic sorter]] or [[odd-even mergesort]].&amp;lt;ref name=&quot;flimsj&quot;&amp;gt;{{cite journal |last1=Papaphilippou |first1=Philippos |last2=Luk |first2=Wayne |last3=Brooks |first3=Chris |title=FLiMS: a Fast Lightweight 2-way Merger for Sorting |journal=IEEE Transactions on Computers |date=2022 |pages=1–12 |doi=10.1109/TC.2022.3146509|hdl=10044/1/95271 |s2cid=245669103 |hdl-access=free }}&amp;lt;/ref&amp;gt; In 2018, Saitoh M. et al. introduced MMS &amp;lt;ref&amp;gt;{{cite book |last1=Saitoh |first1=Makoto |last2=Elsayed |first2=Elsayed A. |last3=Chu |first3=Thiem Van |last4=Mashimo |first4=Susumu |last5=Kise |first5=Kenji |title=2018 IEEE 26th Annual International Symposium on Field-Programmable Custom Computing Machines (FCCM) |chapter=A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath |date=April 2018 |pages=197–204 |doi=10.1109/FCCM.2018.00038|isbn=978-1-5386-5522-1 |s2cid=52195866 }}&amp;lt;/ref&amp;gt; for FPGAs, which focused on removing a multi-cycle feedback datapath that prevented efficient pipelining in hardware. Also in 2018, Papaphilippou P. et al. introduced FLiMS &amp;lt;ref name=&quot;flimsj&quot; /&amp;gt; that improved the hardware utilization and performance by only requiring &amp;lt;math&amp;gt;\log_2(P)+1&amp;lt;/math&amp;gt; pipeline stages of {{math|&#039;&#039;P/2&#039;&#039;}} compare-and-swap units to merge with a parallelism of {{math|&#039;&#039;P&#039;&#039;}} elements per FPGA cycle.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Existing parallel algorithms are based on modifications of the merge part of either the [[bitonic sorter]] or [[odd-even mergesort]].&amp;lt;ref name=&quot;flimsj&quot;&amp;gt;{{cite journal |last1=Papaphilippou |first1=Philippos |last2=Luk |first2=Wayne |last3=Brooks |first3=Chris |title=FLiMS: a Fast Lightweight 2-way Merger for Sorting |journal=IEEE Transactions on Computers |date=2022 |pages=1–12 |doi=10.1109/TC.2022.3146509|hdl=10044/1/95271 |s2cid=245669103 |hdl-access=free &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;|arxiv=2112.05607 &lt;/ins&gt;}}&amp;lt;/ref&amp;gt; In 2018, Saitoh M. et al. introduced MMS &amp;lt;ref&amp;gt;{{cite book |last1=Saitoh |first1=Makoto |last2=Elsayed |first2=Elsayed A. |last3=Chu |first3=Thiem Van |last4=Mashimo |first4=Susumu |last5=Kise |first5=Kenji |title=2018 IEEE 26th Annual International Symposium on Field-Programmable Custom Computing Machines (FCCM) |chapter=A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath |date=April 2018 |pages=197–204 |doi=10.1109/FCCM.2018.00038|isbn=978-1-5386-5522-1 |s2cid=52195866 }}&amp;lt;/ref&amp;gt; for FPGAs, which focused on removing a multi-cycle feedback datapath that prevented efficient pipelining in hardware. Also in 2018, Papaphilippou P. et al. introduced FLiMS &amp;lt;ref name=&quot;flimsj&quot; /&amp;gt; that improved the hardware utilization and performance by only requiring &amp;lt;math&amp;gt;\log_2(P)+1&amp;lt;/math&amp;gt; pipeline stages of {{math|&#039;&#039;P/2&#039;&#039;}} compare-and-swap units to merge with a parallelism of {{math|&#039;&#039;P&#039;&#039;}} elements per FPGA cycle.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Language support ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Language support ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>imported&gt;OAbot</name></author>
	</entry>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=13278&amp;oldid=prev</id>
		<title>imported&gt;CiaPan: /* Application */ adjusting grammatical number of verbs to plural &#039;arrows&#039;</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Merge_algorithm&amp;diff=13278&amp;oldid=prev"/>
		<updated>2024-11-14T11:53:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Application: &lt;/span&gt; adjusting grammatical number of verbs to plural &amp;#039;arrows&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Algorithm that combines multiple sorted lists into one}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Merge algorithms&amp;#039;&amp;#039;&amp;#039; are a family of [[algorithm]]s that take multiple [[sorting algorithm|sorted]] lists as input and produce a single list as output, containing all the elements of the inputs lists in sorted order. These algorithms are used as [[subroutine]]s in various [[sorting algorithm]]s, most famously [[merge sort]].&lt;br /&gt;
&lt;br /&gt;
== Application ==&lt;br /&gt;
[[File:Merge sort algorithm diagram.svg|thumb|upright=1.5|A graph exemplifying merge sort. Two red arrows starting from the same node indicate a split, while two green arrows ending at the same node correspond to an execution of the merge algorithm.]]&lt;br /&gt;
The merge algorithm plays a critical role in the [[merge sort]] algorithm, a [[comparison sort|comparison-based sorting algorithm]]. Conceptually, the merge sort algorithm consists of two steps:&lt;br /&gt;
&lt;br /&gt;
# [[Recursion (computer science)|Recursively]] divide the list into sublists of (roughly) equal length, until each sublist contains only one element, or in the case of iterative (bottom up) merge sort, consider a list of &amp;#039;&amp;#039;n&amp;#039;&amp;#039; elements as &amp;#039;&amp;#039;n&amp;#039;&amp;#039; sub-lists of size 1. A list containing a single element is, by definition, sorted.&lt;br /&gt;
# Repeatedly merge sublists to create a new sorted sublist until the single list contains all elements. The single list is the sorted list.&lt;br /&gt;
&lt;br /&gt;
The merge algorithm is used repeatedly in the merge sort algorithm.&lt;br /&gt;
&lt;br /&gt;
An example merge sort is given in the illustration. It starts with an unsorted array of 7 integers. The array is divided into 7 partitions; each partition contains 1 element and is sorted. The sorted partitions are then merged to produce larger, sorted, partitions, until 1 partition, the sorted array, is left.&lt;br /&gt;
&lt;br /&gt;
== Merging two lists ==&lt;br /&gt;
&lt;br /&gt;
Merging two sorted lists into one can be done in [[linear time]] and linear or constant space (depending on the data access model). The following [[pseudocode]] demonstrates an algorithm that merges input lists (either [[linked list]]s or [[Array data structure|arrays]]) {{mvar|A}} and {{mvar|B}} into a new list {{mvar|C}}.&amp;lt;ref name=&amp;quot;skiena&amp;quot;&amp;gt;{{cite book |last=Skiena |first=Steven |author-link=Steven Skiena |title=The Algorithm Design Manual |publisher=[[Springer Science+Business Media]] |edition=2nd |year=2010 |isbn=978-1-849-96720-4 |page=123}}&amp;lt;/ref&amp;gt;{{r|toolbox}}{{rp|104}} The function {{mono|head}} yields the first element of a list; &amp;quot;dropping&amp;quot; an element means removing it from its list, typically by incrementing a pointer or index.&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;algorithm&amp;#039;&amp;#039;&amp;#039; merge(A, B) &amp;#039;&amp;#039;&amp;#039;is&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;inputs&amp;#039;&amp;#039;&amp;#039; A, B : list&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;returns&amp;#039;&amp;#039;&amp;#039; list&lt;br /&gt;
 &lt;br /&gt;
     C := new empty list&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;while&amp;#039;&amp;#039;&amp;#039; A is not empty and B is not empty &amp;#039;&amp;#039;&amp;#039;do&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; head(A) ≤ head(B) &amp;#039;&amp;#039;&amp;#039;then&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
             append head(A) to C&lt;br /&gt;
             drop the head of A&lt;br /&gt;
         &amp;#039;&amp;#039;&amp;#039;else&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
             append head(B) to C&lt;br /&gt;
             drop the head of B&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;// By now, either A or B is empty. It remains to empty the other input list.&amp;#039;&amp;#039;&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;while&amp;#039;&amp;#039;&amp;#039; A is not empty &amp;#039;&amp;#039;&amp;#039;do&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         append head(A) to C&lt;br /&gt;
         drop the head of A&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;while&amp;#039;&amp;#039;&amp;#039; B is not empty &amp;#039;&amp;#039;&amp;#039;do&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         append head(B) to C&lt;br /&gt;
         drop the head of B&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;return&amp;#039;&amp;#039;&amp;#039; C&lt;br /&gt;
&lt;br /&gt;
When the inputs are linked lists, this algorithm can be implemented to use only a constant amount of working space; the pointers in the lists&amp;#039; nodes can be reused for bookkeeping and for constructing the final merged list.&lt;br /&gt;
&lt;br /&gt;
In the merge sort algorithm, this [[subroutine]] is typically used to merge two sub-arrays {{mono|A[lo..mid]}}, {{mono|A[mid+1..hi]}} of a single array {{mono|A}}. This can be done by copying the sub-arrays into a temporary array, then applying the merge algorithm above.{{r|skiena}} The allocation of a temporary array can be avoided, but at the expense of speed and programming ease. Various in-place merge algorithms have been devised,&amp;lt;ref&amp;gt;{{cite journal |last1=Katajainen |first1=Jyrki |first2=Tomi |last2=Pasanen |first3=Jukka |last3=Teuhola |title=Practical in-place mergesort |journal=Nordic J. Computing |volume=3 |issue=1 |year=1996 |pages=27–40 |citeseerx=10.1.1.22.8523}}&amp;lt;/ref&amp;gt; sometimes sacrificing the linear-time bound to produce an {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039; log &amp;#039;&amp;#039;n&amp;#039;&amp;#039;)}} algorithm;&amp;lt;ref&amp;gt;{{Cite conference| doi = 10.1007/978-3-540-30140-0_63| title = Stable Minimum Storage Merging by Symmetric Comparisons| conference = European Symp. Algorithms| volume = 3221| pages = 714–723| series = Lecture Notes in Computer Science| year = 2004| last1 = Kim | first1 = Pok-Son| last2 = Kutzner | first2 = Arne| isbn = 978-3-540-23025-0| citeseerx=10.1.1.102.4612}}&amp;lt;/ref&amp;gt; see {{slink|Merge sort|Variants}} for discussion.&lt;br /&gt;
&lt;br /&gt;
==K-way merging==&lt;br /&gt;
{{Main|K-way merge algorithm}}&lt;br /&gt;
{{mvar|k}}-way merging generalizes binary merging to an arbitrary number {{mvar|k}} of sorted input lists. Applications of {{mvar|k}}-way merging arise in various sorting algorithms, including [[patience sorting]]&amp;lt;ref name=&amp;quot;Chandramouli&amp;quot;&amp;gt;{{Cite conference |last1=Chandramouli |first1=Badrish |last2=Goldstein |first2=Jonathan |title=Patience is a Virtue: Revisiting Merge and Sort on Modern Processors |conference=SIGMOD/PODS |year=2014}}&amp;lt;/ref&amp;gt; and an [[external sorting]] algorithm that divides its input into {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} {{sfrac|1|&amp;#039;&amp;#039;M&amp;#039;&amp;#039;}} − 1}} blocks that fit in memory, sorts these one by one, then merges these blocks.{{r|toolbox}}{{rp|119–120}}&lt;br /&gt;
&lt;br /&gt;
Several solutions to this problem exist. A naive solution is to do a loop over the {{mvar|k}} lists to pick off the minimum element each time, and repeat this loop until all lists are empty:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;margin-left: 35px; width: 600px&amp;quot;&amp;gt;&lt;br /&gt;
{{framebox|blue}}&lt;br /&gt;
* Input: a list of {{mvar|k}} lists.&lt;br /&gt;
* While any of the lists is non-empty:&lt;br /&gt;
** Loop over the lists to find the one with the minimum first element.&lt;br /&gt;
** Output the minimum element and remove it from its list.&lt;br /&gt;
{{frame-footer}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Best, worst and average case|In the worst case]], this algorithm performs {{math|(&amp;#039;&amp;#039;k&amp;#039;&amp;#039;−1)(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;−{{sfrac|&amp;#039;&amp;#039;k&amp;#039;&amp;#039;|2}})}} element comparisons to perform its work if there are a total of {{mvar|n}} elements in the lists.&amp;lt;ref name=&amp;quot;greene&amp;quot;&amp;gt;{{cite conference |last=Greene |first=William A. |year=1993 |title=k-way Merging and k-ary Sorts |conference=Proc. 31-st Annual ACM Southeast Conf |pages=127–135 |url=http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
It can be improved by storing the lists in a [[priority queue]] ([[heap (data structure)|min-heap]]) keyed by their first element:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;margin-left: 35px; width: 600px&amp;quot;&amp;gt;&lt;br /&gt;
{{framebox|blue}}&lt;br /&gt;
* Build a min-heap {{mvar|h}} of the {{mvar|k}} lists, using the first element as the key.&lt;br /&gt;
* While any of the lists is non-empty:&lt;br /&gt;
** Let {{math|&amp;#039;&amp;#039;i&amp;#039;&amp;#039; {{=}} find-min(&amp;#039;&amp;#039;h&amp;#039;&amp;#039;)}}.&lt;br /&gt;
** Output the first element of list {{mvar|i}} and remove it from its list.&lt;br /&gt;
** Re-heapify {{mvar|h}}.&lt;br /&gt;
{{frame-footer}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Searching for the next smallest element to be output (find-min) and restoring heap order can now be done in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (more specifically, {{math|2⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons{{r|greene}}), and the full problem can be solved in {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039; log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;)}} time (approximately {{math|2&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌊log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌋}} comparisons).{{r|greene}}&amp;lt;ref name=&amp;quot;toolbox&amp;quot;&amp;gt;{{cite book|author1=Kurt Mehlhorn|author-link=Kurt Mehlhorn|author2=Peter Sanders|author2-link=Peter Sanders (computer scientist)|title=Algorithms and Data Structures: The Basic Toolbox |date=2008 |publisher=Springer |isbn=978-3-540-77978-0 |url=http://people.mpi-inf.mpg.de/~mehlhorn/ftp/Toolbox/}}&amp;lt;/ref&amp;gt;{{rp|119–120}}&lt;br /&gt;
&lt;br /&gt;
A third algorithm for the problem is a [[divide and conquer algorithm|divide and conquer]] solution that builds on the binary merge algorithm:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;margin-left: 35px; width: 600px&amp;quot;&amp;gt;&lt;br /&gt;
{{framebox|blue}}&lt;br /&gt;
* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 1}}, output the single input list.&lt;br /&gt;
* If {{math|&amp;#039;&amp;#039;k&amp;#039;&amp;#039; {{=}} 2}}, perform a binary merge.&lt;br /&gt;
* Else, recursively merge the first {{math|⌊&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌋}} lists and the final {{math|⌈&amp;#039;&amp;#039;k&amp;#039;&amp;#039;/2⌉}} lists, then binary merge these.&lt;br /&gt;
{{frame-footer}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When the input lists to this algorithm are ordered by length, shortest first, it requires fewer than {{math|&amp;#039;&amp;#039;n&amp;#039;&amp;#039;⌈log &amp;#039;&amp;#039;k&amp;#039;&amp;#039;⌉}} comparisons, i.e., less than half the number used by the heap-based algorithm; in practice, it may be about as fast or slow as the heap-based algorithm.{{r|greene}}&lt;br /&gt;
&lt;br /&gt;
== Parallel merge ==&lt;br /&gt;
A [[task parallelism|parallel]] version of the binary merge algorithm can serve as a building block of a [[Merge sort#Parallel merge sort|parallel merge sort]]. The following pseudocode demonstrates this algorithm in a [[fork–join model|parallel divide-and-conquer]] style (adapted from Cormen &amp;#039;&amp;#039;et al.&amp;#039;&amp;#039;&amp;lt;ref name=&amp;quot;clrs&amp;quot;&amp;gt;{{Introduction to Algorithms|3}}&amp;lt;/ref&amp;gt;{{rp|800}}). It operates on two sorted arrays {{mvar|A}} and {{mvar|B}} and writes the sorted output to array {{mvar|C}}. The notation {{mono|A[i...j]}} denotes the part of {{mvar|A}} from index {{mvar|i}} through {{mvar|j}}, exclusive.&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;algorithm&amp;#039;&amp;#039;&amp;#039; merge(A[i...j], B[k...ℓ], C[p...q]) &amp;#039;&amp;#039;&amp;#039;is&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;inputs&amp;#039;&amp;#039;&amp;#039; A, B, C : array&lt;br /&gt;
            i, j, k, ℓ, p, q : indices&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;let&amp;#039;&amp;#039;&amp;#039; m = j - i,&lt;br /&gt;
         n = ℓ - k&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; m &amp;lt; n &amp;#039;&amp;#039;&amp;#039;then&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         swap A and B  &amp;#039;&amp;#039;// ensure that A is the larger array: i, j still belong to A; k, ℓ to B&amp;#039;&amp;#039;&lt;br /&gt;
         swap m and n&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; m ≤ 0 &amp;#039;&amp;#039;&amp;#039;then&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         &amp;#039;&amp;#039;&amp;#039;return&amp;#039;&amp;#039;&amp;#039;  &amp;#039;&amp;#039;// base case, nothing to merge&amp;#039;&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;let&amp;#039;&amp;#039;&amp;#039; r = ⌊(i + j)/2⌋&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;let&amp;#039;&amp;#039;&amp;#039; s = binary-search(A[r], B[k...ℓ])&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;let&amp;#039;&amp;#039;&amp;#039; t = p + (r - i) + (s - k)&lt;br /&gt;
     C[t] = A[r]&lt;br /&gt;
 &lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;in parallel do&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
         merge(A[i...r], B[k...s], C[p...t])&lt;br /&gt;
         merge(A[r+1...j], B[s...ℓ], C[t+1...q])&lt;br /&gt;
&lt;br /&gt;
The algorithm operates by splitting either {{mvar|A}} or {{mvar|B}}, whichever is larger, into (nearly) equal halves. It then splits the other array into a part with values smaller than the midpoint of the first, and a part with larger or equal values. (The [[binary search]] subroutine returns the index in {{mvar|B}} where {{math|&amp;#039;&amp;#039;A&amp;#039;&amp;#039;[&amp;#039;&amp;#039;r&amp;#039;&amp;#039;]}} would be, if it were in {{mvar|B}}; that this always a number between {{mvar|k}} and {{mvar|ℓ}}.) Finally, each pair of halves is merged [[Divide and conquer algorithm|recursively]], and since the recursive calls are independent of each other, they can be done in parallel. Hybrid approach, where serial algorithm is used for recursion base case has been shown to perform well in practice &amp;lt;ref name=&amp;quot;vjd&amp;quot;&amp;gt;{{citation| author=Victor J. Duvanenko| title=Parallel Merge| journal=Dr. Dobb&amp;#039;s Journal| date=2011| url=http://www.drdobbs.com/parallel/parallel-merge/229204454}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The [[Analysis of parallel algorithms#Overview|work]] performed by the algorithm for two arrays holding a total of {{mvar|n}} elements, i.e., the running time of a serial version of it, is {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;)}}. This is optimal since {{mvar|n}} elements need to be copied into {{mvar|C}}. To calculate the [[Analysis of parallel algorithms#Overview|span]] of the algorithm, it is necessary to derive a [[Recurrence relation]]. Since the two recursive calls of &amp;#039;&amp;#039;merge&amp;#039;&amp;#039; are in parallel, only the costlier of the two calls needs to be considered. In the worst case, the maximum number of elements in one of the recursive calls is at most &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\frac 3 4 n&amp;lt;/math&amp;gt; since the array with more elements is perfectly split in half. Adding the &amp;lt;math&amp;gt;\Theta\left( \log(n)\right)&amp;lt;/math&amp;gt; cost of the Binary Search, we obtain this recurrence as an upper bound:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;T_{\infty}^\text{merge}(n) = T_{\infty}^\text{merge}\left(\frac {3} {4} n\right) + \Theta\left( \log(n)\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The solution is &amp;lt;math&amp;gt;T_{\infty}^\text{merge}(n) = \Theta\left(\log(n)^2\right)&amp;lt;/math&amp;gt;, meaning that it takes that much time on an ideal machine with an unbounded number of processors.{{r|clrs}}{{rp|801–802}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The routine is not [[Sorting algorithm#Stability|stable]]: if equal items are separated by splitting {{mvar|A}} and {{mvar|B}}, they will become interleaved in {{mvar|C}}; also swapping {{mvar|A}} and {{mvar|B}} will destroy the order, if equal items are spread among both input arrays. As a result, when used for sorting, this algorithm produces a sort that is not stable.&lt;br /&gt;
&lt;br /&gt;
== Parallel merge of two lists ==&lt;br /&gt;
&lt;br /&gt;
There are also algorithms that introduce parallelism within a single instance of merging of two sorted lists. These can be used in field-programmable gate arrays ([[FPGA]]s), specialized sorting circuits, as well as in modern processors with single-instruction multiple-data ([[SIMD]]) instructions. &lt;br /&gt;
&lt;br /&gt;
Existing parallel algorithms are based on modifications of the merge part of either the [[bitonic sorter]] or [[odd-even mergesort]].&amp;lt;ref name=&amp;quot;flimsj&amp;quot;&amp;gt;{{cite journal |last1=Papaphilippou |first1=Philippos |last2=Luk |first2=Wayne |last3=Brooks |first3=Chris |title=FLiMS: a Fast Lightweight 2-way Merger for Sorting |journal=IEEE Transactions on Computers |date=2022 |pages=1–12 |doi=10.1109/TC.2022.3146509|hdl=10044/1/95271 |s2cid=245669103 |hdl-access=free }}&amp;lt;/ref&amp;gt; In 2018, Saitoh M. et al. introduced MMS &amp;lt;ref&amp;gt;{{cite book |last1=Saitoh |first1=Makoto |last2=Elsayed |first2=Elsayed A. |last3=Chu |first3=Thiem Van |last4=Mashimo |first4=Susumu |last5=Kise |first5=Kenji |title=2018 IEEE 26th Annual International Symposium on Field-Programmable Custom Computing Machines (FCCM) |chapter=A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath |date=April 2018 |pages=197–204 |doi=10.1109/FCCM.2018.00038|isbn=978-1-5386-5522-1 |s2cid=52195866 }}&amp;lt;/ref&amp;gt; for FPGAs, which focused on removing a multi-cycle feedback datapath that prevented efficient pipelining in hardware. Also in 2018, Papaphilippou P. et al. introduced FLiMS &amp;lt;ref name=&amp;quot;flimsj&amp;quot; /&amp;gt; that improved the hardware utilization and performance by only requiring &amp;lt;math&amp;gt;\log_2(P)+1&amp;lt;/math&amp;gt; pipeline stages of {{math|&amp;#039;&amp;#039;P/2&amp;#039;&amp;#039;}} compare-and-swap units to merge with a parallelism of {{math|&amp;#039;&amp;#039;P&amp;#039;&amp;#039;}} elements per FPGA cycle.&lt;br /&gt;
&lt;br /&gt;
== Language support ==&lt;br /&gt;
&lt;br /&gt;
Some [[computer language]]s provide built-in or library support for merging sorted [[Collection (abstract data type)|collections]].&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
The  [[C++]]&amp;#039;s [[Standard Template Library]] has the function {{mono|std::merge}}, which merges two sorted ranges of [[iterator]]s, and {{mono|std::inplace_merge}}, which merges two consecutive sorted ranges &amp;#039;&amp;#039;in-place&amp;#039;&amp;#039;. In addition, the {{mono|std::list}} (linked list) class has its own {{mono|merge}} method which merges another list into itself. The type of the elements merged must support the less-than ({{mono|&amp;lt;}}) operator, or it must be provided with a custom comparator.&lt;br /&gt;
&lt;br /&gt;
C++17 allows for differing execution policies, namely sequential, parallel, and parallel-unsequenced.&amp;lt;ref&amp;gt;{{cite web| url=http://en.cppreference.com/w/cpp/algorithm/merge| title=std:merge| publisher=cppreference.com| date=2018-01-08| access-date=2018-04-28}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
[[Python (programming language)|Python]]&amp;#039;s standard library (since 2.6) also has a {{mono|merge}} function in the {{mono|heapq}} module, that takes multiple sorted iterables, and merges them into a single iterator.&amp;lt;ref&amp;gt;{{cite web| url = https://docs.python.org/library/heapq.html#heapq.merge| title = heapq — Heap queue algorithm — Python 3.10.1 documentation}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Merge (revision control)]]&lt;br /&gt;
* [[Join (relational algebra)]]&lt;br /&gt;
* [[Join (SQL)]]&lt;br /&gt;
* [[Join (Unix)]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
== Further reading ==&lt;br /&gt;
* [[Donald Knuth]]. &amp;#039;&amp;#039;[[The Art of Computer Programming]]&amp;#039;&amp;#039;, Volume 3: &amp;#039;&amp;#039;Sorting and Searching&amp;#039;&amp;#039;, Third Edition. Addison-Wesley, 1997. {{ISBN|0-201-89685-0}}. Pages 158–160 of section 5.2.4: Sorting by Merging. Section 5.3.2: Minimum-Comparison Merging, pp.&amp;amp;nbsp;197–207.&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[https://duvanenko.tech.blog/2018/05/23/faster-sorting-in-c/ High Performance Implementation] of Parallel and Serial Merge in [[C Sharp (programming language)|C#]] with source in [https://github.com/DragonSpit/HPCsharp/ GitHub] and in [[C++]] [https://github.com/DragonSpit/ParallelAlgorithms GitHub]&lt;br /&gt;
&lt;br /&gt;
{{sorting}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Merge Algorithm}}&lt;br /&gt;
[[Category:Articles with example pseudocode]]&lt;br /&gt;
[[Category:Sorting algorithms]]&lt;/div&gt;</summary>
		<author><name>imported&gt;CiaPan</name></author>
	</entry>
</feed>