package org.code2care.java.sorting.algo;
import java.util.Arrays;
public class MergeSortJavaExample {
public static void main(String[] args) {
//Unsorted Array
int[] unsortedArray = {4, 99, 2, 11, 34, 67, 54, 12, 45, 245, 234, 12, 200};
MergeSortJavaExample mergeSort = new MergeSortJavaExample();
int[] sortedArray = mergeSort.mergeSortAlgorithm(unsortedArray);
System.out.println(Arrays.toString(sortedArray));
}
public int[] mergeSortAlgorithm(int[] arrayOfNumbers) {
int arrayLength = arrayOfNumbers.length;
if (arrayLength == 1) {
return arrayOfNumbers;
}
//Divide the array into two halves
int midIndex = arrayLength / 2;
int[] leftArray = Arrays.copyOfRange(arrayOfNumbers, 0, midIndex);
int[] rightArray = Arrays.copyOfRange(arrayOfNumbers, midIndex, arrayLength);
leftArray = mergeSortAlgorithm(leftArray);
rightArray = mergeSortAlgorithm(rightArray);
return mergeArrays(leftArray, rightArray);
}
public int[] mergeArrays(int[] leftArray, int[] rightArray) {
int leftLength = leftArray.length;
int rightLength = rightArray.length;
int[] mergedArray = new int[leftLength + rightLength];
int leftIndex = 0, rightIndex = 0, mergedIndex = 0;
while (leftIndex < leftLength && rightIndex < rightLength) {
if (leftArray[leftIndex] <= rightArray[rightIndex]) {
mergedArray[mergedIndex] = leftArray[leftIndex];
leftIndex++;
} else {
mergedArray[mergedIndex] = rightArray[rightIndex];
rightIndex++;
}
mergedIndex++;
}
while (leftIndex < leftLength) {
mergedArray[mergedIndex] = leftArray[leftIndex];
leftIndex++;
mergedIndex++;
}
while (rightIndex < rightLength) {
mergedArray[mergedIndex] = rightArray[rightIndex];
rightIndex++;
mergedIndex++;
}
return mergedArray;
}
}
Output:
Time Complexity | |||||
---|---|---|---|---|---|
Best Case | Average Case | Worst Case | Space Complexity | ||
Merge Sort | Ω(n log n) | Θ(n log n) | O(n log n) | O(n) |
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
More Posts:
- [fix] bash: ssh: command not found - Bash
- How to take user input from the console in a Python program - Python
- How to Enable Macros on Excel for Mac - MacOS
- [JEP 431] Java JDK 21 New Feature - Sequenced Collections - Java-JDK-21
- How to clear Mac Terminal screen - MacOS
- Command to display epoch time Terminal - Linux
- Disable Startup Sound on macOS - MacOS
- ls command to sort by date - Linux