Question

In: Computer Science

Create a Java Application that implements a Selection sort algorithm to sort an array of 20...

Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted by the insertion sort algorithm.

Solutions

Expert Solution

Selection Sort

public class SelectionSort {
        /* Selection Sort function */
        public static void sort(int arr[]){
                int N = arr.length;
                int i, j, pos, temp;
                for (i = 0; i < N - 1; i++) {
                        pos = i;
                        for (j = i + 1; j < N; j++) {
                                if (arr[j] < arr[pos]) {
                                        pos = j;
                                }
                        }
                        temp = arr[i];
                        arr[i] = arr[pos];
                        arr[pos] = temp;
                }
        }
        static void printArray(int arr[]) {
                for(int i=0;i<arr.length;i++)
                        System.out.print(arr[i]+" ");
                System.out.println();
        }
        public static void main(String[] args) {
                int arr[]={121,74,14,66,98,20,53,21,29,49,54,76,54,87,99,14,54,76,8,12};
                System.out.println("Original Array: ");
                printArray(arr);
                sort(arr);
                System.out.println("Sorted Array: ");
                printArray(arr);
        }

}

Insertion Sort

public class InsertionSort {
        /* Function to sort array using insertion sort */
        static void sort(int values[]) {
                int n = values.length;
                for (int i = 1; i < n; ++i) {
                        int key = values[i];
                        int j = i - 1;

                        while (j >= 0 && values[j] > (key)) {
                                values[j + 1] = values[j];
                                j = j - 1;
                        }
                        values[j + 1] = key;
                }
        }

        /* A utility function to print array of size n */
        static void printArray(int arr[]) {
                int n = arr.length;
                for (int i = 0; i < n; ++i)
                        System.out.print(arr[i] + " ");

                System.out.println();
        }

        // Driver method
        public static void main(String args[]) {
                int arr[]={121,74,14,66,98,20,53,21,29,49,54,76,54,87,99,14,54,76,8,12};
                System.out.println("Original Array: ");
                printArray(arr);
                sort(arr);
                System.out.println("Sorted Array: ");
                printArray(arr);
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm:...
This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm: ·Write a function int least(vector<string> strs, int start)to return the index of the smallest value in the vector. You are to assume there is at least one value in the vector. ·Write a function void selsort(vector<string> & strs) to use selection sort to sort the vector of strings. It is a worthwhile experiment to try leaving out the & and seeing that the vector...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT