In: Computer Science
The Problem Statement
In this lab section, you are going to learn how to sort an array of integers, and to sort an array of objects. We are going to divide the work into two parts.
Part 1. Sorting an array of integers using selection sort
In this part, you are given the code (see List 1) for sorting an array of integers into ascending order. The sorting method used is the selection sort. You can cut-and-paste the code into Eclipse and make it work first. List 1 will sort the following array (named myNumbers) of integers:
| 3 | 5 | 1 | 2 | 6 | 7 | 10 | 9 | 8 |
Into ascending order.
Then, you will be asked to re-write the code so it will sort the input array into descending order.
List 1
==============================================================
package ics141lab6;
public class SortingDriver {
public static void main(String[] args) {
int[] myNumbers = {3,5,1,2,6,7,10,9,8};
selectionSort(myNumbers);
for(int i=0;i
System.out.println(myNumbers[i]);
}
}
public static void selectionSort( int[] array ){
for ( int j=0; j
int min = j;
for ( int k=j+1; k
if ( array[k] < array[min] )
min = k;
int temp = array[j];
array[j] = array[min];
array[min] = temp;
}
}
}
==============================================================
Required Work
Rewrite the code in List 1 so it will sort the input array into descending order (use the same array data as input).
SCORE YOUR POINTS BY DOING THE FOLLOWING
Cut and paste your Java source code in the space below (3 pts):
Cut and paste your run output in the space below (4 pts):
Part 2. Sorting an array of objects using insertion sort
In this part, you are asked to write code to sort an array of objects into ascending order. The objects are Strings. The soring algorithm used should be insertion sort. The following is the input array called myStrings
| “cat” | “ dog” | “deer” | “tiger” | “lion” | “rabbit” | “horse” | “snake” | “turtle” |
The structure of the program should be similar to the one in Part 1 except that the algorithm used is insertion sort instead of selection sort. In fact, you can use the code in Part 1 as your starting point. To compare objects, you should use the method compareTo() (we have covered the behavior of this method in the class).
Required Work
Write Java code that uses insertion sort to sort an array of objects. The input array and its content are shown above (see the myStrings above). You are asked to do the following.
SCORE YOUR POINTS BY DOING THE FOLLOWING
1. Cut and paste your Java source code in the space below (4 pts):
2. Cut and paste your run output in the space below. In your output, you should output the initial content of array first. Then, output the array content in ascending order after applied the sorting algorithm (4 pts):
public class SortingDriver {
public static void main(String[] args) {
int[] myNumbers = { 3, 5, 1, 2, 6, 7, 10, 9, 8 };
System.out.println("Before
Sorting: ");
for (int i = 0; i <
myNumbers.length; i++) {
System.out.print(myNumbers[i]+" ");
}
System.out.println();
System.out.println();
selectionSort(myNumbers);
System.out.println("After
Sorting: ");
for (int i = 0; i <
myNumbers.length; i++) {
System.out.print(myNumbers[i]+" ");
}
System.out.println();
System.out.println();
String arr[] = { "cat", "dog",
"deer", "tiger", "lion", "rabbit", "horse", "snake", "turtle"
};
System.out.println("Before
Sorting");
for (String s : arr)
System.out.print(s + " ");
insertionSort(arr);
System.out.println();
System.out.println("\nAfter
Sorting");
for (String s : arr)
System.out.print(s + " ");
}
public static void selectionSort(int[] array) {
for (int j = 0; j < array.length - 1; j++) {
int min = j;
for (int k =
j + 1; k < array.length; k++)
// changed logic here to sort in descending
order
if (array[k] > array[min])
min = k;
int temp = array[j];
array[j] = array[min];
array[min] = temp;
}
}
/**
* @param arr
*/
public static void insertionSort(String arr[]) {
int N = arr.length;
int i, j;
String temp;
for (i = 1; i < N; i++) {
j = i;
temp =
arr[i];
while (j > 0
&& temp.compareTo(arr[j - 1]) < 0) {
arr[j] = arr[j - 1];
j = j - 1;
}
arr[j] =
temp;
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me