In: Computer Science
/** * This program will sort an n by n array by the first value in each row. * Selection sort algorithm is modified to do the sorting. * For example: * <p/> * If the original array is: * 1 2 3 4 5 * 3 4 5 1 2 * 5 2 3 4 1 * 2 3 1 4 5 * 4 2 3 1 5 * <p/> * The array after sorting is: * 1 2 3 4 5 * 2 3 1 4 5 * 3 4 5 1 2 * 4 2 3 1 5 * 5 2 3 4 1 * * @author YOUR NAME * @version 09/29/2020 */ public class ArraySortByFirst { private int[][] dataToSort; public ArraySortByFirst(int[]... data) { this.dataToSort = data; } /** * Task: Sorts an array of integers by the first value of each row. After * sorting, the first column of the array is in ascending order. */ public void sortByFirstColumn() { //TODO Project2 // I am similar to selection sort"); } /** * Task: Finds the row of the smallest value in the first column of this.dataToSort array. * * @param first the index of the first array row to consider * @param last the index of the last array row to consider * @return the index of the row with the smallest element in the first column among * this.dataToSort[first], this.dataToSort[first + 1], . . . , this.dataToSort[last] */ private int getIndexOfSmallest(int first, int last) { int min = this.dataToSort[first][0]; int indexOfMin = first; //TODO Project2 return indexOfMin; } /** * Task: Swaps the rows this.dataToSort[i] and this.dataToSort[j]. * * @param i row to swap * @param j row to swap */ private void swap(int i, int j) { //TODO Project2 } /** * Task: displays the content of this.dataToSort row by row */ public void display() { //TODO Project2 - implement first; } public static void main(String args[]) { int array[][] = {{1, 2, 3, 4, 5}, {3, 4, 5, 1, 2}, {5, 2, 3, 4, 1}, {2, 3, 1, 4, 5}, {4, 2, 3, 1, 5}}; ArraySortByFirst sortArray = new ArraySortByFirst(array); System.out.println("The original array is "); sortArray.display(); System.out.println(); sortArray.sortByFirstColumn(); System.out.println("The array after sorting is "); sortArray.display(); System.out.println(); } // end main } // end ArraySortByFirst
code:
public class ArraySortByFirst {
private int[ ] [ ] dataToSort;
public ArraySortByFirst(int[]....data)
{
this.dataToSort = data;
}
public void sortByFirstColumn()
{
for(int i=0;; i<dataToSort.length; i++)
{
int leastElementRow=getIndexOfSmallest(i,dataToSort.length);
swap(i,leastElementRow);
}
}
private int getIndexOfSmallest(int first,int last)
{
int min=this.dataToSort[first][0];
int indexOfMin= first;
for(int i=first;i<last;i++){
if(dataToSort[i][0]<min){
min=dataToSort[i][0];
indexOfMin=i;
}
return indexOfMin;
}
private void swap(int i,int j)
{
int tempRow[]=dataToSort[i];
dataToSort[i]=dataToSort[j];
dataToSort[j]=tempRow;
}
public void display()
{
for(int i=0;i<dataToSort.length;i++){
for(int j=0;j<dataToSort.length;j++){
System.out.print(dataToSort[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
int array[ ] [ ] ={ {1,2,3,4,5} , {3,4,5,1,2},{5,2,3,4,1} , {2,3,1,4,5},{4,2,3,1,5}};
ArraySortByFirst sortArray= new ArraySortByFirst(array);
System.out.println("The original array is ") ;
sortArray.display();
System.out.println();
sortArray.sortByFirstColumn();
System.out.println("The array after Sorting is ");
sortArray.display();
System.out.println();
}
}
}
screenshot of code :
screenshot of output: