forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_4.java
97 lines (82 loc) · 2.73 KB
/
Exercise_4.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
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int leftArraySize = m+1-l;
int rightArraySize = r-m;
//Your code here
int leftArray[] = new int[leftArraySize];
int rightArray[] = new int[rightArraySize];
for(int i=0; i<leftArraySize; i++)
{
leftArray[i] = arr[i+l]; //creating the left sub array
}
for(int i=0; i<rightArraySize; i++)
{
rightArray[i] = arr[i+m+1]; //creating the right sub array
}
int i=0, j=0, k=l; // initialize k pointer to the lower index of the sub array
while(i<leftArraySize && j<rightArraySize)
{
if(leftArray[i] <= rightArray[j]) //checking whether value in left array is smaller than the value in right array
{
arr[k] = leftArray[i]; //add the smaller value to the final array
i++;
}
else
{
arr[k] = rightArray[j]; //add the smaller value to the final array
j++;
}
k++;
}
while(i<leftArraySize) //left over elements of left sub array is added to the final array
{
arr[k] = leftArray[i];
i++;
k++;
}
while(j<rightArraySize) //left over elements of right sub array is added to the final array
{
arr[k] = rightArray[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
//Write your code here
//Call mergeSort from here
if(l < r)
{
int m = l+(r-l)/2; // find the middle index
sort(arr, l, m); //divide the array into two sub arrays
sort(arr, m+1, r);
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
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);
}
}