Question

In: Computer Science

Using Selection Sort on an any array of size 7, what will be the minimum/maximum number...

Using Selection Sort on an any array of size 7, what will be the minimum/maximum number of comparisons and number of exchanges?

1 for i = 1 to A.length-1

2 for j = i+1 to A.length

3 if A[j] < A[i]

4 exchange A[i] with A[j]

Solutions

Expert Solution

Please find the code below .

In selection sort minimum and maximum number of comparisons and swapping is same in all case like worst best and average.

#include <iostream>
using namespace std;


int main()
{
   int array[7];
   for(int i=0;i<7;i++){
       array[i] = rand()%100;
   }
   cout<<"Before sorting"<<endl;
   for(int i=0;i<7;i++){
       cout<<array[i]<<" ";
   }
   cout<<endl;

   int numberOfComparision = 0;
   int numberOfExchng = 0;
   for(int i=0;i<6;i++){
       for(int j=i+1;j<7;j++){
           numberOfComparision++;
           if(array[j]<array[i]){
               numberOfExchng++;
               int tmp = array[i];
               array[i] = array[j];
               array[j] = tmp;
           }
       }
   }

   cout<<"After sorting"<<endl;
   for(int i=0;i<7;i++){
       cout<<array[i]<<" ";
   }


   cout<<" Number of comparision is : "<<numberOfComparision<<endl;
   cout<<"Number of exchanges is : "<<numberOfExchng<<endl;

   cout<<endl;


   return 0;
}

output:


Related Solutions

Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
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.
What is the maximum number of processes in the system at any time using the following...
What is the maximum number of processes in the system at any time using the following code segment? extern char mypath[]; for ( int i = 0; i < 10; i++ ) { pid_t pid, pid_out; unsigned char status; if ( pid = fork() ) pid_out = wait ( &status ); else   execl ( mypath, "child", "parameter", NULL ); } Assume that child performs some simple computation and returns the result, that is captured in status.
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
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
What is the minimum and maximum number of solutions that we can expect to see in...
What is the minimum and maximum number of solutions that we can expect to see in any given system of nonlinear equations? In your own words, what is the meaning of extraneous solutions? When solving a system of nonlinear equations, is it possible to always use the Addition Method? Explain your reasoning in complete sentences. PLEASE TYPE, DO NOT WRITE IT DOWN and Check your punctuation and proofreading.
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT