简体   繁体   English

4 个排序数组的中值算法

[英]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.我需要为我的课程编写一个算法,以在 O(n) 中找到 4 个不同大小的排序数组的中间值,并且不允许我创建一个数组来存储数据。 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.我想过在一个有 4 个索引的循环上运行,就好像我将数组排序到一个大数组中一样,而是只是在不存储数据的情况下运行。 the loop will stop at n/2 and it should provide me the middle value.循环将在 n/2 处停止,它应该为我提供中间值。 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?写起来似乎很复杂而且很乱(如果我超出范围,我需要检查 4 个数组),有没有更好的方法来解决这个问题?

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) .您可能需要一个函数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.考虑将 4 个数组视为单个未排序的数组,分为 4 个部分。 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).如果您可以修改数组,则可以通过在它们之间交换值来将所有 4 个数组排序为 1(因为您知道 4 个数组已排序,因此可以进行一些优化)。 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.一旦您将数组排序到 n/2(其中 n 是 4 个数组的总长度),只需返回所有 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.我已经实现了getsetlength方法,这是任何数组的基础。 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() .现在需要做的就是使用get(int)set(int,int)length()以及返回中值median()值的方法对类的数据进行排序(可能高达 n/2) 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.通过在中值方法中仅排序最多 n/2,还有进一步优化的空间,在这样做时也为每个元素缓存 (i,j) 对。

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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