Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Selection Sort Algorithm In this laboratory exercise you will implement two

ID: 3819731 • Letter: T

Question


The Selection Sort Algorithm In this laboratory exercise you will implement two variants of the selection sort algorithm. You will add counter variables to count the number of comparisons and swaps. Using successive concatenations, you will generate a string representation of all Boolean expressions evaluated in the program. 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 3 int i, j, temp minIndex 4 for i 0: i list. length 1: i min Index i; for i 1: j list. length: j++) if (list[j] list[minIndex]) min Index j 10 11 temp list[i] 12 list[i] list [min Index] 13 14 list [minIndex] temp 15 16 Y Observe that during every pass, iteration of the outer loop, a swap (Lines 12-14) is carried out.

Explanation / Answer


public class SelectionSortAnalysis {

   public static void main(String[] args) {
       int[] data = {2,4,5,6,4,5,3,5,3};
       System.out.println("UnSorted Data :");
       for(int i=0; i<data.length; i++)
           System.out.print(" "+data[i]);
      
       System.out.println("");
       selectionSort(data);
       System.out.println("Sorted Data :");
       for(int i=0; i<data.length; i++)
           System.out.print(" "+data[i]);
   }
   public static void selectionSort(int[] list){
       int i,j, temp,minIndex;
       int numOfComp = 0;
       int numOfSwap = 0;
       for(i=0; i<list.length-1; i++ ){
           minIndex = i;
           for(j=i+1; j<list.length; j++){
               String comp = "F";
               if(list[j] < list[minIndex]){
                   minIndex = j;
                   comp = "T";
               }
               numOfComp++;
               System.out.println(numOfComp+". "+list[j] +"<" +list[minIndex]+"?("+comp+")");
           }
           if(list[i] != list[minIndex])
               numOfSwap++;
           temp= list[i];
           list[i] = list[minIndex];
           list[minIndex] = temp;
       }
       System.out.println("Comparisons :"+numOfComp+" Swap :"+numOfSwap+" ");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote