Question: Java: Sort an Array of Arrays Overview: Sort a two-dimensional Array of integers. To compare two inner Arrays of integers, use the following algorithm: 1
Java: Sort an Array of Arrays
Overview:
Sort a two-dimensional Array of integers. To compare two inner Arrays of integers, use the following algorithm:
1 find the first differing number between both Arrays
2 use that number for the comparison
3 if no numbers differ, then the Arrays are equal
4 if the length of the Arrays differ, and all of the initial numbers are equal, consider the shorter Array as less
Some examples that illustrate the ordering of two Arrays of integers:
first differing element is second element of both: {{1, 2}, {1, 3}}
first differing element is first element of both: {{0, 5}, {0, 2}}
Array lengths differ, but initial elements are the same: {{7, 6}, {7, 6, 5, 4}}
Setup:
Create a class (.java) file called Sort2DArrays.java
(This can be within a project that contains the other parts of this homework or it can be in separate project on its own)
Features
Implement your program by creating a main method that creates 3 test Arrays:
unsorted {{7,7},{7,1},{1,2,3},{1,5},{1,2},{3,8},{3,8,1},{5}}
already sorted {{1,1},{1,2},{2},{2,4}}
single element {{7}}
For every test Array
print out the original Array
sort it
print out the modified Array
To sort and print, you'll have to implement the following methods:
/**
* Similar to the previous homework on Array Utilities...
*
* Converts a two dimension Array of ints into a String
* by using the following format:
*
* The entire Array is surrounded by curly braces.
* Each inner Array is surrounded by curly braces.
* Each inner Array is separated by commas.
* Every number in the inner Array is separated by commas.
*
* Example:
*
* {{1,2},{1,2,3,4},{2},{2,3}}
*
* @param arr
* @return a String representation of the Array
*/ public static String arrayToString(int[][] arr)
/**
* Sorts a 2 dimension Array of int[] Arrays in place
* (no return value). Implement a version of selection
* sort to do this.
*
* @param arr the Array to be sorted
*/ public static void sort(int[][] arr)
/**
* Determine of the first Array of ints passed in is
* equal to, greater than or less than the second
* Array of ints.
*
* @param arr1 the first Array of ints in comparison
* @param arr2 the second Array of ints in comparison
* @return 0 if arr1 and arr2 are equal
* 1 if arr1 is greater than arr2
* -1 if arr1 is less than arr2
*/ public static int arrayCompare(int[] arr1, int[] arr2)
Example Output
original:{{7,7},{7,1},{1,2,3},{1,5},{1,2},{3,8},{3,8,1},{5}}
sorted: {{1,2},{1,2,3},{1,5},{3,8},{3,8,1},{5},{7,1},{7,7}}
original: {{1,1},{1,2},{2},{2,4}}
sorted: {{1,1},{1,2},{2},{2,4}}
original: {{7}}
sorted: {{7}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
