Java public class Sort public static void sortint list int

Java

public class Sort

{

public static void sort(int[] list)

{

int j; // index of smallest value

for (int i=0; i

{

j = indexOfNextSmallest(list, i);

swap(list, i, j);

}

} // end sort

//****************************************************************

private static int indexOfNextSmallest(

int[] list, int startIndex)

{

int minIndex = startIndex; // index of smallest value

for (int i=startIndex+1; i

{

if (list[i] < list[minIndex])

{

minIndex = i;

}

} // end for

return minIndex;

} // end indexOfNextSmallest

//****************************************************************

private static void swap(int[] list, int i, int j)

{

int temp; // temporary holder for number

temp = list[i];

list[i] = list[j];

list[j] = temp;

} // end swap

} // end Sort

public class SortDriver {

   public static void main(String[] args)

   {

       int[] studentIds = {3333, 1234, 2222, 1000};

   Sort.sort(studentIds);

   for (int i=0; i

   {

   System.out.print(studentIds[i] + \" \");

   }

   }//end main

}//end SortDriver

Modify the code below to handle an array of strings and sort them A sample array: Jane Bob Susan Keith Edward Adam Your driver will need to display the unsorted and then the sorted array. Compile and test your code A sample run Sort2Driver Unsorted array: Jane Bob Susan Keith Edward Adam Sorted array: Adam Bob Edward Jane Keith Susan

Solution

In order to sort the string array, you can use a very simple method of Array class :

Arrays.sort(array);

Below is the code as per your requirement :

public class Sort
{
public static void sort(String[] list)
{
System.out.println(\"Unsorted array :\");
for(String s :list)
{
System.out.print(\" \"+s);
}
Arrays.sort(list);
System.out.println(\"\ Sorted array :\");
for(String s :list)
{
System.out.print(\" \"+s);
}
} // end sort method

} // end Sort class

public class SortDriver {
public static void main(String[] args)
{
String strArray={\"Jane\",\"Bob\",\"Susan\",\"Keith\",\"Edward\",\"Adam\"};
Sort.sort(strArray);
for(String s :strArray)
{
System.out.print(\" \"+s);
}
}//end main
}//end SortDriver

Java public class Sort { public static void sort(int[] list) { int j; // index of smallest value for (int i=0; i { j = indexOfNextSmallest(list, i); swap(list,
Java public class Sort { public static void sort(int[] list) { int j; // index of smallest value for (int i=0; i { j = indexOfNextSmallest(list, i); swap(list,
Java public class Sort { public static void sort(int[] list) { int j; // index of smallest value for (int i=0; i { j = indexOfNextSmallest(list, i); swap(list,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site