Question

In: Computer Science

/** * This program will sort an n by n array by the first value in...

/**
 * 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

Solutions

Expert Solution

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:


Related Solutions

Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
Write an ARMv8 program to sort an array of elements. As Imentioned in class, this...
Write an ARMv8 program to sort an array of elements. As I mentioned in class, this problem uses a portion of your Programming Assignment 1 where you computed the smallest and largest values in an array. Here we will extend that assignment to find the “index” of the smallest and the index of the largest elements of the array. The following C code segment illustrates how we can sort the element of an array.For this problem, assume an array with...
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Draft a program that scans an alphanumeric array to determine the first alphabet in the array.  If...
Draft a program that scans an alphanumeric array to determine the first alphabet in the array.  If found, the program should print “alphabet found” its value and index.  If no alphabet is found, the program should print “no alphabet found.” Submit the asm/list file and screenshots that show the output of your code for the following example arrays: a. Array has only alphabets b. Array has only numerals c. Several arrays with a mix of alphabets and numerals positioned at different indices
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT