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.
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)              ...
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++*
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...
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT