i don’t understand this particular sample code from geeksforgeeks on k-way merge sort. More specifically, I don’t understand what the "n" in the for loop in the base case for divide() is. Is it the number of students in each array? (which in this case is 4) Also could you please explain what the for […]

Categories

## k-way merge sort base case

- Post author By Full Stack
- Post date September 20, 2020
- No Comments on k-way merge sort base case

- Tags (l + r) / 2, 1, 15, 17, 18, 4, 7, 7 } }; // Number of arrays int k = arr.length; // Output array int[] output = new int[n * k];, 8, 9, arr); // merge the left and right half merge(l, arr); // Print merged array for (int i = 0; i < n * k; i++) System.out.print(output[i] + " "); } }, arr); // to sort right half divide((l + r) / 2 + 1, i don't understand this particular sample code from geeksforgeeks on k-way merge sort. More specifically, I don't understand what the "n" in the for loop in the base case for divide() is. Is it the number of students in each array? (which in this, int arr[][]) { if (l == r) { /* base step to initialize the output array before performing merge, int r, int[] output, int[] output) { // To store the starting point // of left and right array int l_in = l * n, k: 1, merge the k arrays. Create a recursive function which will take k arrays and divide them into two parts and call the function recursively wit, Output, output); } // Driver Code public static void main(String[] args) { // input 2D-array int arr[][] = {, r, r_arr[] = new int[r_c]; // storing data in left array for (int i = 0; i < l_c; i++) l_arr[i] = output[l_in, r_curr = 0; // to store the current index // for output array int in = l_in; // two pointer merge f, r_in = ((l + r) / 2 + 1) * n; // to store the size of left and // right array int l_, right part will start from the index ((l + r) / 2 + 1) * n of the output array. import java.util.*; class GFG { static int n = 4, they will be sorted and stored in the output array. After that, we call the left and right half of the range recursively so that, we merge the left and right half. For merging, we need to determine the range of indexes for the left and right halves in the output array. We can easily find that. Left part will start fr