Question

In: Computer Science

   private static void merge(int arr[], int l, int m, int r) {        //...

   private static void merge(int arr[], int l, int m, int r) {
       // Find sizes of two subarrays to be merged
       int n1 = m - l + 1;
       int n2 = r - m;

       /* Create temp arrays */
       int L[] = new int[n1];
       int R[] = new int[n2];

       /* Copy data to temp arrays */
       for (int i = 0; i < n1; ++i)
           L[i] = arr[l + i];
       for (int j = 0; j < n2; ++j)
           R[j] = arr[m + 1 + j];

       /* Merge the temp arrays */

       // Initial indexes of first and second subarrays
       int i = 0, j = 0;

       // Initial index of merged subarry array
       int k = l;
       while (i < n1 && j < n2) {
           if (L[i] <= R[j]) {
               arr[k] = L[i];
               i++;
           } else {
               arr[k] = R[j];
               j++;
           }
           k++;
       }

       /* Copy remaining elements of L[] if any */
       while (i < n1) {
           arr[k] = L[i];
           i++;
           k++;
       }

       /* Copy remaining elements of R[] if any */
       while (j < n2) {
           arr[k] = R[j];
           j++;
           k++;
       }
   }

   /**
   * Merge sort
   *
   * @param arr - Integer array
   * @param l -Left index
   * @param r - right index
   */
   private static void mergeSort(int arr[], int l, int r) {
       if (l < r) {
           // Find the middle point
           int m = (l + r) / 2;

           // Sort first and second halves
           mergeSort(arr, l, m);
           mergeSort(arr, m + 1, r);

           // Merge the sorted halves
           merge(arr, l, m, r);
       }
   }

How to count number of swaps and comparisions in the above code?

Solutions

Expert Solution

We can see that both swap and comparison operations are happening in merge function. So we can create two global variable count_swaps and count_comparisons initialized to 0, and on every comparison the value of count_comparisons will increment by 1 and on every swap count_swaps will increment by 1.

We can keep the variable count_comparisons just below the while loop which is "while (i < n1 && j < n2)" , here every iteration of while loop is happen with one comparison and hence we increment count_comparisons on every iteration of while loop. Now swap happen if within this while loop i.e. "while (i < n1 && j < n2)" , whenever the if condition i.e.  "if (L[i] <= R[j])" is satisfied then L[i] will be placed before R[i] and swapping will not happen but if this condition is not satisfied then R[j] will placed before L[i] and hence swap happen, so increment the count_swaps by 1.

So below is the modification of while loop "while (i < n1 && j < n2)" inside which we increment both count_comparisons and count_swaps.

while (i < n1 && j < n2) {

count_comparisons++ ; //increment by 1
           if (L[i] <= R[j]) {
               arr[k] = L[i];
               i++;
           } else {

count_swaps++; //increment by 1
               arr[k] = R[j];
               j++;
           }
           k++;
       }

Hence at the end of algorithm value of count_swaps and count_comparisons will be desired answer.

Please comment for any clarification.


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int)...
FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int) * fin); ***************************************************** If I want to open a in.txt, only the integer in the file, is the malloc code correct? What if the in.txt includes the string and integer, how can I use the malloc?   Thanks
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT