0

I have the following insertion sort algorithm but I have a problem understanding the recursive method mergeSort.

public class MergeSort {

    public static void main(String[] args) {
        int[]intArray = {20, 35, -15, 7, 55, 1, -22};
        mergeSort(intArray , 0 , intArray.length);
        for(int i = 0 ; i < intArray.length ; i++) {
            System.out.println(intArray[i]);
        }
    }

    public static void mergeSort(int[] input, int start, int end) {
        System.out.println("start = " + start + " ||" + " end = " + end + " ||" + " mid = " +((start+end)/2));
        if (end - start < 2) {
            return;
        }
        int mid = (start + end) / 2;
        mergeSort(input, start, mid);
        mergeSort(input, mid, end );
        merge(input, start, mid, end);
    }

    public static void merge(int[] input, int start, int mid, int end) {
        if (input[mid - 1] <= input[mid]) {
            return;
        }
        int i = start;
        int j = mid;
        int tempIndex = 0;
        int[] temp = new int[end -start];
        while (i < mid && j < end) {
            temp[tempIndex++] = input[i] <= input[j] ? input[i++] : input[j++];
        }
        System.arraycopy(input, i, input, start + tempIndex, mid-i);
        System.arraycopy(temp, 0, input, start, tempIndex);
    }
}

The output is:

start = 0 || end = 7 || mid = 3
start = 0 || end = 3 || mid = 1
start = 0 || end = 1 || mid = 0
start = 1 || end = 3 || mid = 2
start = 1 || end = 2 || mid = 1
start = 2 || end = 3 || mid = 2
start = 3 || end = 7 || mid = 5
start = 3 || end = 5 || mid = 4
start = 3 || end = 4 || mid = 3
start = 4 || end = 5 || mid = 4
start = 5 || end = 7 || mid = 6
start = 5 || end = 6 || mid = 5
start = 6 || end = 7 || mid = 6

-22 , -15 , 1 , 7 , 20 , 35 , 55 , 

I can't understand how the variable start, in method mergeSort, is changing if don´t have something like

start = start+1

The same goes for variable end.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Does this answer your question? [Understanding recursion](https://stackoverflow.com/questions/717725/understanding-recursion) – Karl Knechtel Sep 25 '22 at 02:35
  • Basically when you call a method like this: `mergeSort(input , start , mid);` you are supplying new values for `start` and `end`, which is the same as using `=` to reassign them. – markspace Sep 25 '22 at 02:43
  • i the example case the variable is decresing (flowersInVase - 1) i don't see a plus sign in the method mergeSort but is increasing anyway – Cristopher Vergara Sep 25 '22 at 02:44
  • It's `int mid = (start + end)/2 ;` that does it. `mid` is more than `start` and less than `end`, so that value is used to both increase and decrease. Calling `(mid,end)` will increase `start`, and calling `(start,mid)` will decrease `end` – markspace Sep 25 '22 at 02:51
  • A method call does the assignment. You don't see it because it is behind the scenes. Let's say you have a method `int foo (int a, int b);`. Your program has `int bin = foo (bar, bug);`. Behind the scenes, the value of `bar` gets copied to the `a` in the `foo` method,and `bug` gets copied to the `b`. Now, suppose you have have a program that needs to call `foo` 4 times, with unrelated values: `foo (19, 87)`, `foo (flea, dog)`, `foo (side * side, height)`, `foo ( smu, smu)`. By linking the variables or constants in the argument list with the variables in the parameter list allows that. – Old Dog Programmer Sep 25 '22 at 03:03
  • 2
    Passing a value to a function using a parameter isn't really related to recursion. This is the same behavior as a normal function call. – ggorlen Sep 25 '22 at 06:10
  • If the call is ...(start, mid), then within the function, end = mid, and used for recursive calls. Likewise if the call is ...(mid, end), then start = mid, and used for recursive calls. – rcgldr Sep 26 '22 at 02:38

0 Answers0