The Selection Sort Algorithm In this laboratory exercise you

The Selection Sort Algorithm In this laboratory exercise you will implement two variants of the selection sort algorithm The Sorter Class Define a class called Sorter that consists of one method, the implementation of the selection sort algorithm below. Provide Javadoc documentation for both the class and the method. Listing 1: An Implementation of the Selection Sort Algorithm 1 public static void selectionSort (int[] list) 2 T 3 int i, j. temp, minIndex; for (i = 0; i

Solution

Hope this helps....

/* package whatever; // don\'t place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be \"Main\" only if the class is public. */
class Sorter
{
   public static void selectionSort(int []list)
   {
       System.out.print(\"Initial Data \");
       System.out.println(Arrays.toString(list));
       int i,j,temp,minindex;
       System.out.println();
       int totcomp=0,totswap=0;
       for(i=0;i<list.length-1;i++)
       {
           minindex=i;
           int compa=1,swapp=0;
           compa++;
       //   compa++;
           System.out.println(Arrays.toString(list));  
           for(j=i+1;j<list.length;j++)
           {
               compa++;
               compa++;
               if(list[j]<list[minindex])
               {
                   //compa++;
               minindex=j;
               }
           }
           if(list[i]!=list[minindex])
           {
           swapp++;
           compa++;
           temp=list[i];
           list[i]=list[minindex];
           list[minindex]=temp;
           }
           totswap+=swapp;
           totcomp+=compa;
          
           System.out.println(\"# of comparisions: \"+compa+\",swaps: \"+swapp);
           System.out.println();
          
       }
           System.out.println(\"Total # of comparisions: \"+totcomp+\",swaps: \"+totswap);
   }

   }
class SelectionSortDemo
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int a[]=new int[] {2,4,5,6,4,5,3,5,3};
   //   int b[];
       Sorter s=new Sorter();
       s.selectionSort(a);
       System.out.println(Arrays.toString(a));
   }
}

 The Selection Sort Algorithm In this laboratory exercise you will implement two variants of the selection sort algorithm The Sorter Class Define a class called
 The Selection Sort Algorithm In this laboratory exercise you will implement two variants of the selection sort algorithm The Sorter Class Define a class called

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site