Skip to content

Create merge sort #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions merge sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Arrays;

class Main
{
// merge arrays : intArray[start...mid] and intArray[mid+1...end]
public static void merge(int[] intArray, int[] temp, int start, int mid, int end)
{
int k = start, i = start, j = mid + 1;

// traverse through elements of left and right arrays
while (i <= mid && j <= end) {
if (intArray[i] < intArray[j]) {
temp[k++] = intArray[i++];
} else {
temp[k++] = intArray[j++];
}
}

// Copy remaining elements
while (i <= mid) {
temp[k++] = intArray[i++];
}

// copy temp array back to the original array to reflect sorted order
for (i = start; i <= end; i++) {
intArray[i] = temp[i];
}
}
// sorting intArray[low...high] using iterative approach
public static void mergeSort(int[] intArray)
{
int low = 0;
int high = intArray.length - 1;

// sort array intArray[] using temporary array temp
int[] temp = Arrays.copyOf(intArray, intArray.length);

// divide the array into blocks of size m
// m = [1, 2, 4, 8, 16...]
for (int m = 1; m <= high - low; m = 2*m)
{
for (int i = low; i < high; i += 2*m)
{
int start = i;
int mid = i + m - 1;
int end = Integer.min(i + 2 * m - 1, high);
//call merge routine to merge the arrays
merge(intArray, temp, start, mid, end);
}
}
}

public static void main(String[] args)
{
//define array to be sorted
int[] intArray = { 10,23,-11,54,2,9,-10,45 };
//print the original array
System.out.println("Original Array : " + Arrays.toString(intArray));
//call mergeSort routine
mergeSort(intArray);
//print the sorted array
System.out.println("Sorted Array : " + Arrays.toString(intArray));
}
}