
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Iterative Merge Sort
In this article, we will learn about iterative Merge sort in Java. Unlike the traditional iterative Merge Sort, the following Java program implements a recursive merge step while dividing the array and an iterative approach for merging.
What is merge sort?
Merge Sort is a popular sorting algorithm that follows the Divide and Conquer rule. It recursively divides the array into two halves, sorts each half, and then merges them back in a sorted manner.
The following are three main steps to perform Merge sort in Java ?
-
Divide: The array is split into two halves recursively until individual elements remain.
-
Conquer: Each half is sorted using recursive Merge Sort calls.
- Merge: The two sorted halves are merged back into a single sorted array.
Iteratively Sort the Elements Using Merge Sort
In this approach, we recursively divide the array into smaller subarrays until each contains a single element. Then, we iteratively merge these sorted subarrays back into the original array in a sorted manner.
Pseudocode
Following is the Pseudocode for the merge sort function ?
MERGE(left, right, array) 1. Initialize three pointers: i = 0, j = 0, k = 0 2. While i < length(left) AND j < length(right): - If left[i] < right[j], array[k] = left[i], increment i, k - Else array[k] = right[j], increment j, k 3. While i < length(left), copy remaining left elements to array 4. While j < length(right), copy remaining right elements to array
The following are the steps to iteratively sort the elements using merge sort ?
- The merge_sort() function recursively divides the array into two halves until the base condition (single-element arrays) is reached.
- The left and right halves are sorted recursively.
- The sorted halves are merged back into the original array.
- The merging process ensures elements from both halves are inserted in a sorted manner.
Example
Following is the Java program for Iterative Merge Sort ?
import java.util.Arrays; public class Demo{ public static void merge_sort(int[] my_arr){ if(my_arr == null){ return; } if(my_arr.length > 1){ int mid = my_arr.length / 2; int[] left = new int[mid]; for(int i = 0; i < mid; i++){ left[i] = my_arr[i]; } int[] right = new int[my_arr.length - mid]; for(int i = mid; i < my_arr.length; i++){ right[i - mid] = my_arr[i]; } merge_sort(left); merge_sort(right); int i = 0; int j = 0; int k = 0; while(i < left.length && j < right.length){ if(left[i] < right[j]){ my_arr[k] = left[i]; i++; } else { my_arr[k] = right[j]; j++; } k++; } while(i < left.length){ my_arr[k] = left[i]; i++; k++; } while(j < right.length){ my_arr[k] = right[j]; j++; k++; } } } public static void main(String[] args){ int my_arr[] = {56, 78, 91, 21, 34, 0, 11}; int i=0; merge_sort(my_arr); System.out.println("The array after sorting is "); for(i=0; i<my_arr.length; i++) System.out.print(my_arr[i]+" "); } }
Output
The array after sorting is 0 11 21 34 56 78 91
Time complexity: The time complexity of Merge Sort is O(n log n) in all cases.
Space complexity: The space complexity is O(n) due to the extra space required for subarrays.
Conclusion
This Java program efficiently sorts an array using Merge Sort. Though it uses recursion, it follows an iterative merge step, making it a hybrid approach. Merge Sort is particularly useful for large datasets due to its predictable time complexity and stability in sorting.