-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.java
More file actions
69 lines (60 loc) · 2.07 KB
/
mergeSort.java
File metadata and controls
69 lines (60 loc) · 2.07 KB
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
class Solution {
// Function to merge two sorted halves of the array
public void merge(int[] arr, int low, int mid, int high) {
// Temporary array to store merged elements
List<Integer> temp = new ArrayList<>();
int left = low;
int right = mid + 1;
// Loop until subarrays are exhausted
while (left <= mid && right <= high) {
// Compare left and right elements
if (arr[left] <= arr[right]) {
// Add left element to temp
temp.add(arr[left]);
// Move left pointer
left++;
} else {
// Add right element to temp
temp.add(arr[right]);
// Move right pointer
right++;
}
}
// Adding the remaining elements of left half
while (left <= mid) {
temp.add(arr[left]);
left++;
}
// Adding the remaining elements of right half
while (right <= high) {
temp.add(arr[right]);
right++;
}
// Transferring the sorted elements to arr
for (int i = low; i <= high; i++) {
arr[i] = temp.get(i - low);
}
}
// Helper function to perform merge sort from low to high
public void mergeSortHelper(int[] arr, int low, int high) {
// Base case: if the array has only one element
if (low >= high)
return;
// Find the middle index
int mid = (low + high) / 2;
// Recursively sort the left half
mergeSortHelper(arr, low, mid);
// Recursively sort the right half
mergeSortHelper(arr, mid + 1, high);
// Merge the sorted halves
merge(arr, low, mid, high);
}
// Function to perform merge sort on the given array
public int[] mergeSort(int[] nums) {
int n = nums.length; // Size of array
// Perform Merge sort on the whole array
mergeSortHelper(nums, 0, n - 1);
// Return the sorted array
return nums;
}
}