I\'m trying to create a fast running O(nlgn) array sort, consisting of 3 rows an
ID: 3850215 • Letter: I
Question
I'm trying to create a fast running O(nlgn) array sort, consisting of 3 rows and N columns, that will give me the largest common number among all 3 rows. I'm getting stuck on how to implement sink() while having a 2d array. There is test code provisws that it needs to pass.
MAIN CODE
import java.util.Arrays;
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) {
int row, colm;
int [][] Temp = new int[row][colm];
Temp = a;
if (a == null || a.length == 0){
return 0;
}
int [] Row1 = a[0];
int [] Row2 = a[1];
int [] Row3 = a[2];
Arrays.sort(a);
int n = a.length-1;
for(int k = n/2; k >= 1; k--)
{
sink(max, k, n);
}
while (n > 1)
SortUtils.swap(a, 1, n--);
sink(max, 1, n);
}
}
TEST CODE
package edu.csus.csc130.spring2017.assignment2;
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);
}
}
Explanation / Answer
package edu.csus.csc130.spring2017.assignment2;
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);
}
}