-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.java
58 lines (50 loc) · 1.41 KB
/
merge_sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//MERGE SORT: SORTING IN ASCENDING
//ARRAY KA MID NIKALO,
public class Merge_sort {
public static void printArr(int arr[]) {
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i] + " ");
}
System.out.println();
}
public static void mergeSort(int arr[], int start, int end) {
if (start >= end) {
return; // base
}
int mid = start + (end - start) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
public static void merge(int arr[], int start, int mid, int end) {
int temp[] = new int[end - start + 1];
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end) {
if (arr[i] < arr[j]) {
temp[k] = arr[i];
i++;
} else {
temp[k] = arr[j];
j++;
}
k++;
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= end) {
temp[k++] = arr[j++];
}
for (k = 0, i = start; k < temp.length; k++, i++) {
arr[i] = temp[k];
}
}
public static void main(String args[]) {
int arr[] = { 0, 9, 8, 7, 1, 2, 5, 7 };
mergeSort(arr, 0, arr.length - 1);
printArr(arr);
}
}
//Time complexity = nlogn