Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a java program named SelectionSort.java, implement a method as shown belo

ID: 3593917 • Letter: C

Question

 Create a java program named SelectionSort.java, implement a method as shown below:  public void sort(String[][] names, int flag) where String[][] names is a Nx2 two dimensional array with each row has two elements representing first name and last name. This method sorts names based on first name or last name which is specified with the flag value, if the flag equals 1, it sorts based on first name, for all other values, it sorts according to last names.  A data file is provided for testing. The first line is a number N followed by N lines with each line represents a name of a person.  How to run sample:  type *data.txt 1 or *data.txt 2 in the Cmd argument list 
JAVA.
  Create a java program named SelectionSort.java, implement a method as shown below:    public void sort(String[][] names, int flag)  where String[][] names is a Nx2 two dimensional array with each row has two elements representing first name and last name.  This method sorts names based on first name or last name which is specified with the flag value, if the flag equals 1, it sorts based on first name, for all other values, it sorts according to last names.    A data file is provided for testing. The first line is a number N followed by N lines with each line represents a name of a person.    How to run sample:    type *data.txt 1 or *data.txt 2 in the Cmd argument list  
JAVA.

Explanation / Answer

Please find my implementation.

public void sort(String[][] names, int flag) {

       int n = names.length;

       int sortIndex = 0;

       if(flag != 1)

           sortIndex = 1;

       // One by one move boundary of unsorted subarray

       for (int i = 0; i < n-1; i++)

       {

           // Find the minimum element in unsorted array

           int min_idx = i;

           for (int j = i+1; j < n; j++)

               if (names[sortIndex][j].compareTo(names[sortIndex][min_idx]) < 0)

                   min_idx = j;

           // Swap the found minimum element with the first

           // element

           String temp = names[sortIndex][min_idx];

           names[sortIndex][min_idx] = names[sortIndex][i];

           names[sortIndex][i] = temp;

           //swapping other names

           temp = names[(sortIndex+1)%2][min_idx];

           names[(sortIndex+1)%2][min_idx] = names[(sortIndex+1)%2][i];

           names[(sortIndex+1)%2][i] = temp;

       }

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote