Java public class Sort { public static void sort(int[] list) { int j; // index o
ID: 3940009 • Letter: J
Question
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 SusanExplanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.