Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge sort application

I'm doing an exercise where given an array of N values, I need to get the two numbers that their subtraction (the highest - the lowest) is the most positive number. I want v to be larger than c... the case is that... let's say I want to buy auctions at price C so I can sell them at price V and get the maximum profit, and each cell of the array is the price of that auction at day t, so I want to buy at the lowest price possible so I can sell at the highest price possible so C must appear before V in the array. For example:

n = 8
arr = {6,7,3,8,9,2,1,4,20}

I want c = 1 and v = 20, because 20 - 1 = 19 (it means the subtraction from this 2 numbers is the highest)

Another example:

n = 6
arr = {8,12,45,40,18,29}

I want c = 8 and v = 45 because their subtraction is the highest number of all the other subtractions. (I want to clarify that c is not always the smallest number in the array). BOTH NUMBERS DO NOT NEED TO BE NEXT TO EACH OTHER. If I have n = 1, {1} then c = 1 and v = 1.

This example demonstrates c and v are not always the lowest/highest values.

n = 6
arr = {19,27,5,6,7,8}

In this case c = 19 and v = 27

Also, I need to solve this using merge sort's code many (examples divide it by two methods: mergesort which handles the recursions, and merge that does the change of positions using an aux array).

I'm using mergesort code(merge is unnecessary in my opinion because I don't care about sorting), so far I have the following code, but it is obviously wrong, could someone tell me what I'm not doing right?

public static void mergeSort(int start, int end) {
    if(start < end) {
        int half = (start + end) / 2;
        mergeSort(start, half);
        for(int i = start; start < half; start++, i++){
            if((arr[i+1] - arr[i]) > temp){
                temp = arr[i+1] - arr[i];
                c = i;
                v = i+1;
            }
        }
        mergeSort(half+1, end);
        for(int i = half+1; i < end; half++, i++){
            if((arr[i+1] - arr[i]) > temp){
                temp = arr[i+1] - arr[i];
                c = i;
                v = i+1;
            }
        }
    }
}

Thanks in advance for any help you can offer!

like image 502
Ignacio Pochart Avatar asked Jan 18 '26 00:01

Ignacio Pochart


1 Answers

I guess the name mergeSort in your code is inherited....

As you have already do the recursion, there is no need to iterate through all the elements, because after recursion, the result is already presented. For example, one possible way is to swap the minimum to the first place and the maximum to the last place, and later, on the "upper" level of recursion you can just retrieve them directly.


Here is another solution which takes advantage of the philosophy of merge-sort, but only returns max.

public class test {
    static int arr [] = {6,7,3,8,9,2,1,4,20};

    public static void main (String args[]) {
        System.out.println(merge_select_max(0, arr.length - 1));
    }

    public static int merge_select_max (int start, int end) { // both inclusive
        if (start == end) {
            return arr[start];          
        }
        else {
            int half = (start + end) / 2;
            int first = merge_select_max (start, half);
            int second = merge_select_max (half + 1, end);
            return (first > second ? first : second);           
        }       
    }
}
like image 98
Dante May Code Avatar answered Jan 19 '26 14:01

Dante May Code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!