Skip to content

Commit

Permalink
Merge pull request #62 from arshadpatel/myfeature
Browse files Browse the repository at this point in the history
Implemented Algorithm of Merge Sort in Java #45
  • Loading branch information
mrsamirr authored Oct 22, 2023
2 parents f68db38 + 24b0526 commit 250317b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@

- GitHub: https://github.com/Mansiprajapatii
```

```markdown
#### Name: Arshad M. Patel

- GitHub: https://github.com/arshadpatel
```
58 changes: 58 additions & 0 deletions JAVA/MergeSortAlgo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
public class MergeSortAlgo {
public static void main(String[] args) {
int[] arr = { 2, 7, 8, 9, 1, 3, 4, 5 };
int low = 0;

int high = arr.length - 1;
mergeSort(arr, low, high);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

}

static void mergeSort(int arr[], int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
mergefunc(arr, low, mid, high);
}
}

static void mergefunc(int[] arr, int low, int mid, int high) {
int n1 = mid + 1 - low;
int n2 = high - 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 i = 0; i < n2; i++) {
right[i] = arr[mid + 1 + i];
}
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
k++;
} 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++;
}
}
}

0 comments on commit 250317b

Please sign in to comment.