Skip to content
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

Create MergeSort.java #124

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
72 changes: 72 additions & 0 deletions SORTING AND SEARCHING/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

public class MergeSort {

public static void main(String args[]) {
int arr[] = {23, 12, 85, 39, 98, 7, 16, 97, 54};

System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array");
printArray(arr);
}

void merge(int arr[], int low, int mid, int up) {
int n1 = mid - low + 1;
int n2 = up - mid;

int Left[] = new int[n1];
int Right[] = new int[n2];

for (int i = 0; i < n1; ++i)
Left[i] = arr[low + i];
for (int j = 0; j < n2; ++j)
Right[j] = arr[mid + 1 + j];

int i = 0, j = 0;

int k = low;
while (i < n1 && j < n2) {
if (Left[i] <= Right[j]) {
arr[k] = Left[i];
i++;
}
else {
arr[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = Left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = Right[j];
j++;
k++;
}
}

void sort(int arr[], int low, int up) {
if (low < up) {
int mid =low + (up-low)/2;

sort(arr, low, mid);
sort(arr, mid + 1, up);

merge(arr, low, mid, up);
}
}

static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}