JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Azam Vsmd asked this:
// https://www.sololearn.com/Discuss/1011099/?ref=app
// how recursive function works in merge sort
// Sounded like something I wanted to learn so I got an algorithm from here:
// https://en.m.wikipedia.org/wiki/Merge_sort?wprov=sfla1
// I added a lot of comments to describe the code, a minor improvement to
// remove 4 extra variable references, the main method to setup & start the
// sort, and debugging to display what is going on as it happens.
public class Main {
//**************************************************
// Make a copy of the source array range in the destination array (same range).
static void CopyArray(int A[], int iBegin, int iEnd, int B[]) {
for (int k = iBegin; k < iEnd; k++)
B[k] = A[k];
} // CopyArray
//**************************************************
// Merge the source array in left and right halves into the destination. The
// left source half is A[iBegin:iMiddle-1]. The right source half is A[iMiddle:iEnd-1].
// The destination result is B[iBegin:iEnd-1].
static void TopDownMerge(int A[], int iBegin, int iMiddle, int iEnd, int B[]) {
// Display debugging information and setup the indexes to the current element
// in both halves.
System.out.format("TopDownMerge: processing %d to %d%n", iBegin, iEnd-1);
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run