Question

In: Computer Science

This lab will focus on creating a better understanding of Selection Sort and Insertion Sort algorithms....

This lab will focus on creating a better understanding of Selection Sort and Insertion Sort algorithms.

What you need to do

I have provided a driver and a Utility class with three methods. You must finish writing Selection Sort and Insertion Sort in the Utility class. Below is pseudocode for Selection and Insertion Sort, which you may use as a guide. Your selection sort will sort an array of Strings lexicographically, meaning A-Z. Your insertion sort will sort an array of Strings in reverse, meaning Z-A.

                      Selection Sort Pseudo Code

for i = 0 to length(A) min = i

for j = i to length(A) if A[j] < A[min]

min = j end for

                           swap A[i] and A[min]
                      end for
                      Insertion Sort Pseudo Code

for i = 1 to length(A) j=i

while j > 0 and A[j-1] < A[j] swap A[j] and A[j-1]

j=j-1 end while

end for

How do we compare strings to sort them correctly? Use the method compareTo provided in the string class. This method returns a int value and is used as follows:

<str1>.compareTo(<str2>)

If str1 comes before str2, compareTo will return a negative number. If str1 comes after str2, compareTo will return a positive number.
If str1 and str2 are the same, compareTo will return 0.

Driver Output

Unsorted array:
Tom, Steve, Ann, Zoe, Bob, Moana, Naomi, Kevin, Ryan, Nina, Dora, Wanda, Eric

Selection Sorted array from A-Z:
Ann, Bob, Dora, Eric, Kevin, Moana, Naomi, Nina, Ryan, Steve, Tom, Wanda, Zoe

Insertion Sorted array from Z-A:
Zoe, Wanda, Tom, Steve, Ryan, Nina, Naomi, Moana, Kevin, Eric, Dora, Bob, Ann

Driver.java///////

public class Driver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       String[] names = {"Tom", "Steve","Ann","Zoe","Bob","Moana","Naomi","Kevin","Ryan","Nina","Dora","Wanda","Eric"};
      
       System.out.println("Unsorted array:");
       Utility.printArray(names);
       System.out.println();
      
       Utility.selectionSort(names);
       System.out.println("Selection Sorted array from A-Z:");
       Utility.printArray(names);
       System.out.println();

       Utility.insertionSort(names);
       System.out.println("Insertion Sorted array from Z-A:");
       Utility.printArray(names);
      
   }

}


Utility.java///////

public class Utility {

   public static void selectionSort(String[] array) {
       //TODO - Sort array from A-Z
   }
  
   public static void insertionSort(String[] array) {
       //TODO - Sort array from Z-A
   }
  
   public static void printArray(String[] array) {
      
       for(int i = 0; i < array.length; i++) {
           System.out.print(array[i]);
           if(i != array.length -1) {
               System.out.print(", ");
           }
       }
       System.out.println();
   }
  
}

Solutions

Expert Solution

Answer:

Driver.java:

public class Driver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] names = {"Tom", "Steve","Ann","Zoe","Bob","Moana","Naomi","Kevin","Ryan","Nina","Dora","Wanda","Eric"};

        System.out.println("Unsorted array:");
        Utility.printArray(names);
        System.out.println();

        Utility.selectionSort(names);
        System.out.println("Selection Sorted array from A-Z:");
        Utility.printArray(names);
        System.out.println();

        Utility.insertionSort(names);
        System.out.println("Insertion Sorted array from Z-A:");
        Utility.printArray(names);

    }

}


Utility.java:

public class Utility {

    public static void selectionSort(String[] array) {
        //TODO - Sort array from A-Z
        int n = array.length;

        // One by one move boundary of unsorted subarray
        for (int i=0; i<n-1; i++)
        {
            // Find the minimum element in unsorted array
            for (int j=i+1; j<n; j++)
            {
                if (array[i].compareTo(array[j]) > 0)
                {
                    // Swap the found minimum element with the first
                    // element
                    String temp=array[j];
                    array[j]=array[i];
                    array[i]=temp;
                }
            }
        }
    }


    public static void insertionSort(String[] array) {
        //TODO - Sort array from Z-A
        int n = array.length;
        int i,j;
        for (i = 1; i < n; i++)
        {

           String temp = array[i];
            j=i;
            while (i > 0 && array[i - 1].compareTo(temp) < 0)
            {
                array[i]=array[i-1];
                i--;
            }
            array[i]=temp;
        }
    }

    public static void printArray(String[] array) {

        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if(i != array.length -1) {
                System.out.print(", ");
            }
        }
        System.out.println();
    }

}

Output:


Related Solutions

In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a...
This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a text data file containing information on tutorial websites for a variety of programming languages. The data file is named Tutorials. It contains records of programming tutorial websites. The record structure for this text file is: FIELD 1 = Programming Language FIELD 2 = Name and Description of Website FIELD 3 = URL Web Address of Language Tutorial The structure of the file is that...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort....
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort. Write a Pseudo code first for each of these sort methods. After writing the pseudo code write python code from the pseudo code.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT