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

Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
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...
public static int example3 (int [] arr ) { int n = arr.length , total =...
public static int example3 (int [] arr ) { int n = arr.length , total = 0; for (int j = 0; j < n ; j += 2) for (int k = 0; k <= j ; k ++) total += arr [ j ]; return total ; } what is the running time of this method?
Iterative Merge Sort is not working #include #include #include int* b; void merge(int a[], int left,...
Iterative Merge Sort is not working #include #include #include int* b; void merge(int a[], int left, int mid, int right) {    int i = left, j = mid + 1, k = left;    while (i <= mid && j <= right)    {        if (a[i] < a[j])            b[k++] = a[i++];        else            b[k++] = a[j++];    }    for (; i <= mid; i++)    {        b[k++]...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6; int a[ACOUNT] = {1, 2, 3, 4, 5}; double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[CCOUNT] = "HELLO"; cout <<"Array A contents: \n"; arrayContents(a, ACOUNT); cout <<"Array B contents: \n"; arrayContents(b, BCOUNT); cout <<"Array C contents: \n"; arrayContents(c, CCOUNT); return 0; } template void arrayContents(const T *arr, int countSize)...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT