Question

In: Computer Science

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

Solutions

Expert Solution

import java.util.Scanner;

public class Sort {
   public static int swapCount = 0;
   public static void swap(int arr[] ,int i,int j) {
       int temp=arr[i];
       arr[i] = arr[j];
       arr[j] =temp;
   }
  
   public static void selectionSort(int a[]){
       int min;
       int n = a.length;
       for(int i=0;i<n-1;i++){
           min=i;
           for(int j=i+1;j<n;j++)if(a[j]<a[min])min=j;
           if(min!=i){
               swapCount++;
               swap(a , i,min);
           }
       }
   }
  
   public static void main(String args[]) {
       int n;
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter size of array: ");
       n = scan.nextInt();
       int arr[] = new int[n];
       System.out.print("Enter elements in array: ");
       for(int i=0;i<n;i++) {
           arr[i] = scan.nextInt();
       }
       selectionSort(arr);
       System.out.println("Number of swaps "+ swapCount);
       scan.close();
   }
  
   /* when i=0 , inner for Loop runs (1 to n-1) n-1 times
   * when i=1 , inner for Loop runs (2 to n-1) n-2 times
   * when i=2 , inner for Loop runs (3 to n-1) n-3 times and so on...........
   * Total run = (n-1) + (n-2) + (n-3) + ..............+ 2 + 1
   * T(n) = n*(n-1)/2
   * T(n) = O(n^2)
   * /
}


Related Solutions

Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
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
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) {...
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...
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.
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort...
c++ For your program, you will choose either the Selection Sort algorithm or the Insertion Sort algorithm and create a recursive implementation of that algorithm. Your program should: Randomly generate an array of at least 20 values. Display the contents of that (unsorted) array. Use the recursive implementation of either Selection or Insertion Sort to sort the values. Display the contents of the now sorted array, to demonstrate the success of the algorithm.
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT