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

This is Selection Sort Java code. Please add comment on each line of code to des

ID: 3792282 • Letter: T

Question

This is Selection Sort Java code. Please add comment on each line of code to describe what they doing

public class SelectionSort {

      

      public static int[] SelectionSort(int[] arr){

        

         System.out.print("Sorted list: ");

           

          for (int i = 0; i < arr.length - 1; i++)

          {

              int index = i;

              for (int j = i + 1; j < arr.length; j++)

                  if (arr[j] < arr[index])

                      index = j;

        

              int smallerNumber = arr[index];

              arr[index] = arr[i];

              arr[i] = smallerNumber;

          }

          return arr;

      }

      

      public static void main(String arr[]){

           

          int[] arr1 = {14,13,11,12,9,8,2,0};

          int[] arr2 = SelectionSort(arr1);

          for(int i:arr2){

              System.out.print(i);

              System.out.print(", ");

          }

      }

   }

Explanation / Answer

public class SelectionSort /*class selection sort*/

public static int[] SelectionSort(int[] arr)

/*function that performs selection sort*/

{

         System.out.print("Sorted list: ");

          for (int i = 0; i < arr.length - 1; i++)/*to traverse throughout the array*/

          {

              int index = i;

/* To find smallest number in index*/

              for (int j = i + 1; j < arr.length; j++)

                  if (arr[j] < arr[index])

                      index = j;

/*swap smallest number and arr[index]*/

              int smallerNumber = arr[index];

              arr[index] = arr[i];

              arr[i] = smallerNumber;

          }

          return arr;

      }

/*main method*/

      public static void main(String arr[]){

/*set of inputs*/

          int[] arr1 = {14,13,11,12,9,8,2,0};

/* Calling selection sort function*/

          int[] arr2 = SelectionSort(arr1);

/*print sorted array*/

          for(int i:arr2){

              System.out.print(i);

              System.out.print(", ");

          }

      }

   }

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