Sorting algorithms/Merge sort: Difference between revisions

Content deleted Content added
MaiconSoft (talk | contribs)
m Added Delphi reference to Pascal code
Alextretyak (talk | contribs)
Added 11l
Line 64: Line 64:
Note: &nbsp; better performance can be expected if, rather than recursing until &nbsp; <big> length(m) ≤ 1, </big> &nbsp; an insertion sort is used for &nbsp; <big> length(m) </big> &nbsp; smaller than some threshold larger than &nbsp; '''1'''. &nbsp; However, this complicates the example code, so it is not shown here.
Note: &nbsp; better performance can be expected if, rather than recursing until &nbsp; <big> length(m) ≤ 1, </big> &nbsp; an insertion sort is used for &nbsp; <big> length(m) </big> &nbsp; smaller than some threshold larger than &nbsp; '''1'''. &nbsp; However, this complicates the example code, so it is not shown here.
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python}}

<lang 11l>F merge(left, right)
[Int] result
V left_idx = 0
V right_idx = 0
L left_idx < left.len & right_idx < right.len
I left[left_idx] <= right[right_idx]
result.append(left[left_idx])
left_idx++
E
result.append(right[right_idx])
right_idx++

I left_idx < left.len
result.extend(left[left_idx ..])
I right_idx < right.len
result.extend(right[right_idx ..])
R result

F merge_sort(m)
I m.len <= 1
R m

V middle = m.len I/ 2
V left = m[0.<middle]
V right = m[middle..]

left = merge_sort(left)
right = merge_sort(right)
R Array(merge(left, right))

V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
print(merge_sort(arr))</lang>

{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
Line 236: Line 277:
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
</pre>
</pre>

=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}