Public Methods
mergeSort
The main entry point for sorting an integer array using the merge sort algorithm.
MergeSort.java:6-21
Parameters
The integer array to be sorted. The array is sorted in-place in ascending order.Valid inputs:
- Any integer array with length ≥ 0
- Can contain duplicates
- Can contain negative numbers
nullis handled gracefully (method returns immediately)
Return Value
This method does not return a value. The input array is modified in-place to be sorted in ascending order.
Behavior
Edge Cases
Edge Cases
The method handles several edge cases automatically:
- Null array: Returns immediately without error
- Empty array: Returns immediately (already sorted)
- Single element: Returns immediately (already sorted)
- Duplicate elements: Handles correctly with stable sorting
Algorithm Steps
Algorithm Steps
- Validates that the array is not null and has more than one element
- Creates a temporary array of the same size for merging operations
- Calls
mergeSortReordenamiento()with initial bounds (0 to length-1) - The original array is sorted in-place upon completion
Example Usage
Time Complexity
Even if the array is already sorted, merge sort always divides and merges.
The array is divided log n times, and each merge operation is O(n).
Merge sort has consistent performance regardless of input order.
Space Complexity
Requires a temporary array of the same size as the input array for merging.
The recursive calls create a stack depth of log n levels.
Private Methods
mergeSortReordenamiento
Recursive method that implements the divide-and-conquer strategy, splitting the array and coordinating the merge operations.
MergeSort.java:23-52
Parameters
The original array being sorted. This array is modified in-place.
A temporary array of the same size used for merging operations. Must have the same length as
originalArreglo.The starting index (inclusive) of the subarray to sort. Valid range: 0 to array.length - 1.
The ending index (inclusive) of the subarray to sort. Valid range: indiceInicio to array.length - 1.
Behavior
Base Case:- Calculate the midpoint:
mitadArreglo = indiceInicio + (indiceFin - indiceInicio) / 2 - Recursively sort the left half: indices
[indiceInicio, mitadArreglo] - Recursively sort the right half: indices
[mitadArreglo + 1, indiceFin] - Merge the two sorted halves using the
merge()method - Print the current state of the subarray (educational output)
merge
Merges two sorted subarrays into a single sorted subarray within the original array.
MergeSort.java:54-103
Parameters
The original array containing the two sorted subarrays to be merged. Modified in-place.
Temporary storage array used during the merge operation. Must have the same length as
originalArreglo.The starting index of the first sorted subarray.
The ending index of the first sorted subarray. The second subarray starts at
mitadArreglo + 1.The ending index of the second sorted subarray.
Behavior
The merge operation works in three phases:Copy to Temporary Array
Copy the entire range
[indiceInicio, indiceFin] from originalArreglo to depositoArreglo.Merge Sorted Halves
Use three pointers to merge the two sorted halves:
contadorPrimerArreglo: Tracks position in left half[indiceInicio, mitadArreglo]contadorSegundoArreglo: Tracks position in right half[mitadArreglo + 1, indiceFin]acompladorArreglos: Tracks where to place the next element inoriginalArreglo
Three-Pointer Algorithm
Why no second cleanup loop? If the right half has remaining elements after the main merge loop, they’re already in their correct positions in
originalArreglo. Only the left half needs explicit copying.Example Merge Operation
Before merge:Main Method
main
Entry point for testing and demonstrating the merge sort implementation.
MergeSort.java:105-139
Current Implementation
The main method demonstrates sorting with a predefined array:Alternative Implementation (Commented Out)
The code includes an alternative implementation using random arrays:Random Array Generation Code
Random Array Generation Code
Complete Class Summary
Public Interface
1 public method
mergeSort(int[] arreglo)
Private Helpers
2 private methods
mergeSortReordenamiento(...)- Divisionmerge(...)- Merging
Entry Point
1 main method
main(String[] args)
No State
0 instance variablesAll methods are static - no object instantiation needed.
Usage Patterns
- Direct Usage
- Integration
- Testing
For practical examples and output demonstrations, see the Examples page.