Question

In: Computer Science

The Problem Statement In this lab section, you are going to learn how to sort an...

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

  1. Cut and paste your Java source code in the space below (3 pts):

  1. 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):

 

Solutions

Expert Solution

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


Related Solutions

Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched...
Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched pairs T-test for a population mean using StatCrunch. We will work with a data set that has historical importance in the development of the T-test. Paired T hypothesis test: μD = μ1 - μ2 : Mean of the difference between Regular seed and Kiln-dried seed H0 : μD = 0 HA : μD > 0 Hypothesis test results: Difference Mean Std. Err. DF T-Stat...
In this lab you will learn how to use methods from the Math class in order...
In this lab you will learn how to use methods from the Math class in order to calculate the area or the volume of several different shapes. If you are confused about the Methods you can access from the Math class and would like to see some examples click here. Hint: Most of these methods can be done in one line. Step 1 - circleArea In this method you will calculate the area of a circle. Before you can calculate...
In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting...
In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting language. The student will have to do research on the Bourne Shell Scripting language and then writ a script that asks the First Name , Last Name, Age, and Country of origin of the student and then print out these items in a statement Then the students will write a 3 to 4-page paper (not including the title and references pages) describing how the...
Goal: in this lab, you will learn to configure sudo to allow a user mrussell to...
Goal: in this lab, you will learn to configure sudo to allow a user mrussell to change password for users. Please follow the steps and answer all the questions at the end of the lab instruction. In the Linux machine ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. For this part you will need a 2nd normal user (non-root) account. If you don't have an account for "auser" with password “room1202” yet, you can create one by running (as room1202): $ sudo useradd -c "A User"...
How should students prepare to use chemicals in the lab? Select one or more: Sort the...
How should students prepare to use chemicals in the lab? Select one or more: Sort the lab chemicals in alphabetical order for quick access. Become familiar with the chemicals to be used, including exposure or spill hazards. Check labels and discard chemicals that are not 100% pure. Locate the spill kits and understand how they are used.
Function Template and Exception Handling In part one, you are going to implement the selection sort...
Function Template and Exception Handling In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception. You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file: Part one: unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of...
The aim of this lab is to learn the way of converting an ARRAY to a...
The aim of this lab is to learn the way of converting an ARRAY to a SET and the other way around. To complete this lab we need to remember the implementation of the Set Interface, HashSet Class, and Arrays. Build an array with some elements (String, Integer, or Float). Then initialize a set (using HashSet) with the elements of the converted array. To transfer an Array into a Set, firstly we have to convert it to a List using...
Problem 21-12 Please explain how you come to the answers, I'm hoping to learn how to...
Problem 21-12 Please explain how you come to the answers, I'm hoping to learn how to do them. Thank you!! You have been assigned to examine the financial statements of Picard Corporation for the year ended December 31, 2020, as prepared following IFRS. Picard uses a periodic inventory system. You discover the following situations: 1. The physical inventory count on December 31, 2019, improperly excluded merchandise costing $27,700 that had been temporarily stored in a public warehouse. 2. The physical...
PROBLEM 1) You are a scientist in a lab that studies how cell fates are specified...
PROBLEM 1) You are a scientist in a lab that studies how cell fates are specified during development. In particular, you are curious about muscle development. Muscle cells are derived from the mesoderm (one of the three primary germ layers) and are specified rather early in development. You are using the fruit fly, Drosophila melanogaster, as a model system to study the phenomenon of muscle fate specification. You have isolated a line of flies carrying a loss of function mutation...
Please let me know how to make code sort. If you put sort (sort 1000 6...
Please let me know how to make code sort. If you put sort (sort 1000 6 5 4 3 2 1, not separate), you will get 1 2 3 4 5 6 1000. sort 50 300 27 5 5 27 50 300 public static void main (String[] args) {        Scanner scnr = new Scanner(System.in);        String a = "";            a = scnr.nextLine();            String[] b = imput.split(" ") if (b[0].equalsI("sort")) { }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT