简体   繁体   中英

Median Algorithm for 4 sorted arrays

I need to write an Algorithm for my course, to find the middle value of 4 sorted arrays different sizes in O(n), and i'm not allowed to create an array to store the data. how should I approach the problem? I thought about running on a loop with 4 indexes as if i'm sorting the arrays into a big array but instead just run without storing the data. the loop will stop at n/2 and it should provide me the middle value. writing it seems complex and very messy (i need to check for 4 of the arrays if i'm out of bound), is there a better way to approach this?

I think you're on to the key idea: the median value of all the values in four arrays is just the median of all the values, so if we get half way through all the values then whatever is next is the median. I would suggest structuring as follows:

int firstIndex = 0;
int secondIndex = 0;
int thirdIndex = 0;
int fourthIndex = 0;
double current;

for (int i = 0; i < n/2; i++) {
    // 1.) Find the value out of the four at firstIndex, secondIndex, ...
    //     which is smallest, and assign it to current
    // 2.) Increment whichever of the four indices belongs to that element
}
// whatever is in current at the end of the loop is the middle element

You probably want a function findMin(int index1, int index2, int index3, int index4) . This method could also be responsible for the out-of-bounds checks, so the main loop could just rely on it to be pointed in the right direction, and not care if it's run out of elements in any given array.

Does this make sense? I've tried to leave enough ambiguity to let you handle most of the real implementation work :)

Think of a single unsorted array

Consider thinking about the 4 arrays as a single unsorted array, broken into 4 parts. If you can modify the arrays, you can sort all 4 arrays as if 1 by swapping values between them (some optimizations can be made since you know the 4 arrays are sorted). Once you've sorted the arrays up to n/2 (where n is the aggregate length of the 4 arrays), just return the middle value of all 4.

Some Code

The implementation below begins to make multiple arrays function like a single one. I've implemented get , set , and length methods, the basis for any array. All that needs to happen now is for the class' data to be sorted (possibly up to n/2) using get(int) , set(int,int) , and length() , and a method which returns the median value median() .

There is also room for further optimization by sorting only up to n/2 within the median method, also when caching (i,j) pairs for each element when doing so.

int median( int[] a1, int[] a2, int[] a3, int[] a4 ) {
    MultiIntArray array = new MultiIntArray( a1, a2, a3, a4 );
    array.sort();
    return array.get( array.length() / 2 );
}
public class MultiIntArray {

    private int[][] data;

    public MultiIntArray( int[]... data ) {
        this.data = data;
    }

    public void sort() {
        // FOR YOU TO IMPLEMENT
    }

    public int length() {
        int length = 0;
        for ( int[] array : data ) {
            length += array.length;
        }
        return length;
    }

    public int get( int index ) {
        int i = 0;
        while ( index >= data[i].length ) {
            index -= data[i].length;
            i += 1;
        }
        return data[i][index];
    }

    public void set( int index, int value ) {
        int i = 0;
        while ( index >= data[i].length ) {
            index -= data[i].length;
            i += 1;
        }
        data[i][index] = value;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM