处理2D阵列

时间:2017-03-12 20:46:24

标签: java arrays sorting matrix comparison

因此,我正在尝试实现一种排序算法,该算法将采用3行和N列的矩阵,并将行从最小到最大排序,总体目标是在排序行中查找常见的最大数字。但是,由于我需要实现数组的方式,我不确定如何调整它以便我可以只检查行。因为我的测试文件是作为矩阵发送的,所以我的算法不允许我做任何事情。

import java.util.Arrays;
import java.util.PriorityQueue;

public class FindCommon {

/*
 * @param a 3xN integer array, assume a is not null or empty
 * @return the largest common number among a[0], a[1], a[2],  null if no  common number exists
 */
public static Integer getLargestCommonNumber(int[][] a) {

    //Check if array is empty
    if (a == null || a.length == 0){
        return 0;
    }
    //Initialize
    int [] Row1 = a[0];
    int [] Row2 = a[1];
    int [] Row3 = a[2];

    int maxHeap, minHeap;

    int [][] temp = a;
    int n = a.length-1;

    Arrays.sort(a);

    //Heap sort
    for(int k = n/2; k >= 1; k--)
    {
        sink(a, k, n);
    }
    while (n > 1)
        SortUtils.swap(a,  1, n--);
        sink(a, 1, n);

    //Comparison



}
}

测试代码

import org.junit.Assert;
import org.junit.Test;

public class FindCommonTest {

@Test
public void testGetLargestCommonNumber1() {
    int[][] a = {{54, 41, 43, 55, 63}, {25, 40, 48, 12, 89}, {20, 19, 90, 94, 52}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertNull(result);
}

@Test
public void testGetLargestCommonNumber2() {
    int[][] a = {{53, 41, 43, 55, 63}, {41, 25, 48, 12, 54}, {91, 19, 90, 54, 41}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertEquals(41, (int) result);
}   

@Test
public void testGetLargestCommonNumber3() {
    int[][] a = {{54, 41, 43, 55, 63}, {25, 41, 48, 12, 54}, {41, 19, 90, 54, 94}};
    Integer result = FindCommon.getLargestCommonNumber(a);
    Assert.assertEquals(54, (int) result);
}   

}

0 个答案:

没有答案