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

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...
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]...
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
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...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30)...
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30) + 21; int s = 0; while(n>1){ if(n & 1){ n = (n<<2) - n + 1; } else { n = n >> 1; } if(s == t && m == t){ return; } s++; } failure("Seems you forgatz the essence of Collatz"); } Hash value: 2002296385 what input values should n, m have in order for the function phase05 to work?
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT