-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathMergeSortRecursion.java
107 lines (93 loc) · 2.56 KB
/
MergeSortRecursion.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//merge sort using divide and conquer and recursion
import java.util.Scanner;
class MergeSortRecursion
{
public static void sort(int arr[], int start, int end)
{
if(start==end) //base case
return;
else
{
// Find the mid of the array
int mid = start + ((end - start) / 2);
// Sort first and second halves
sort(arr, start, mid);
sort(arr, mid + 1, end);
merge(arr,start, mid, end); // Merge the sorted halves
}
}
public static void merge(int arr[], int start, int mid, int end)
{
// Find sizes of two subarrays to be merged
int nL = mid - start + 1; //left size
int nR = end - mid; //right size
/* Create temporary arrays */
int L[] = new int[nL]; //left array
int R[] = new int[nR]; //right array
/*Copy data to temporary arrays*/
for (int i = 0; i < nL; ++i)
L[i] = arr[start + i];
for (int j = 0; j < nR; ++j)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0;
// Initial index of merged subarry array
int k = start;
while (i < nL && j < nR) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] */
while (i < nL) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] */
while (j < nR) {
arr[k] = R[j];
j++;
k++;
}
}
public static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of array"+"\n"+" n = ");
int n=sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Inputted array : ");
printArray(arr);
sort(arr, 0, n-1);
System.out.println("\nMergeSorted array : ");
printArray(arr);
}
}
/* Sample Input And Output :
* Enter size of array
n = 5
33 47 19 2 101
Inputted array :
33 47 19 2 101
MergeSorted array :
2 19 33 47 101
Time Complexity :
Worst case: O(n*log n)
Best Case: O(n*log n)
Space Complexity : O(n)
*/