Skip to main content

Run Your First MergeSort

Get the MergeSort algorithm running on your machine and see it in action. This guide will walk you through compiling, running, and understanding the output.
1

Navigate to the source directory

First, navigate to the directory containing MergeSort.java:
cd workspace/source
2

Compile the program

Compile the Java source file using javac:
javac MergeSort.java
Make sure you have Java Development Kit (JDK) 8 or higher installed. Check your version with java -version.
This creates a MergeSort.class file containing the compiled bytecode.
3

Run the program

Execute the compiled program:
java MergeSort
You’ll see the algorithm process the array step by step!

Understanding the Output

When you run the program, you’ll see detailed output showing how merge sort divides and conquers the array.

Example Output

With the default array {10, 5, 8, 9, 2, 3, 1, 6}, you’ll see:
Proceso de ordenamiento en subarreglo: 0 a 0
10 
Proceso de ordenamiento en subarreglo: 1 a 1
5 
Proceso de ordenamiento en subarreglo: 0 a 1
5 10 
Proceso de ordenamiento en subarreglo: 2 a 2
8 
Proceso de ordenamiento en subarreglo: 3 a 3
9 
Proceso de ordenamiento en subarreglo: 2 a 3
8 9 
Proceso de ordenamiento en subarreglo: 0 a 3
5 8 9 10 
...
1 2 3 5 6 8 9 10
Each line shows a subarray being sorted. Watch how smaller sorted subarrays merge into larger ones!

Modify the Input Array

Want to try different arrays? Edit the main method in MergeSort.java:
public static void main(String[] args) {
    // Try your own array!
    int[] originalArreglo = {10, 5, 8, 9, 2, 3, 1, 6};
    
    mergeSort(originalArreglo);
    
    for (int num : originalArreglo) {
        System.out.print(num + " ");
    }       
}
The random array example is already in the source file as commented code. Just uncomment it to test with random values!

Using the MergeSort Method

The public mergeSort() method is the entry point for sorting any integer array.

Method Signature

public static void mergeSort(int[] arreglo)

Example Usage

// Create an unsorted array
int[] numbers = {42, 17, 93, 8, 56};

// Sort it in-place
mergeSort(numbers);

// The array is now sorted: {8, 17, 42, 56, 93}
The mergeSort() method sorts the array in-place, meaning it modifies the original array rather than returning a new one.

Key Features

Edge Case Handling

Automatically handles null arrays and single-element arrays without errors.

Visual Process Output

See each merge operation with detailed console output showing subarray progression.

Efficient Algorithm

O(n log n) time complexity for optimal sorting performance on larger datasets.

Educational Design

Detailed Spanish comments explain each step of the divide-and-conquer process.

Next Steps

Now that you have MergeSort running, dive deeper into understanding the algorithm:

Algorithm Overview

Learn how the merge sort algorithm works under the hood

API Reference

Explore all methods and implementation details

Build docs developers (and LLMs) love