Many Comparisons Made Selection Sorting Algorithm Input 0 100 Order Q37216809

How many comparisons are made in a selection sortingalgorithm with a input of 0-100 that is in order?


Answer


For a array with 0-100 integer in an order in selection sortalgorithm  , their will 5050 comparision will be done.

Code –

import java.util.Scanner;

public class Main
{
public static void selectionSort( int arr[] ){
//variable declared , comparsion to count number of comparsiondone
int comparsion = 0;
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++)
{
//increment comparsion
comparsion++;
if (arr[j] < arr[pos]){
pos = j;
}
}
temp = arr[i];
arr[i] = arr[pos];
arr[pos]= temp;
}   
//print the comparsion value
System.out.println(“Comparision “+comparsion);
}
/* Main

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.