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

For this SelSort code below - I having trouble tracing it correctly! Please writ

ID: 3772863 • Letter: F

Question

For this SelSort code below - I having trouble tracing it correctly!

Please write detailes steps on how to trace it and what are the results - show work please!

// Selection Sort

public class SelSort {

public static void main(String[] args) {

   final int SIZE = 5;

   int [] x = {89, 2, -13, 67, 0};

   for (int i=0; i<x.length; i++) {

      // find smallest element in elements i to x.length

      int currentMin = x[i];

      int minIndex = i;

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

       if (x[j] < currentMin) {

          currentMin = x[j];

          minIndex = j;

       }

      }

      // swap smallest element with element i

      if (minIndex != i) {

       x[minIndex] = x[i];

       x[i] = currentMin;

      }

   }

   System.out.println("Sorted array: ");

   for (int i=0; i<x.length; i++) {

      System.out.println(x[i]);

   }

}

}

I have finished the loop when i=0 and found -13 and swapped it BUT I start to lose it when i=1

please see pics below and tell me what should I do!

Explanation / Answer

Selection sort simply takes first element and compares it with rest of the elements in the array and if there is any element less than this selected element then swap the locations.

For example in your case the array is {89, 2, -13, 67, 0};

Take first element, and initially store in MinVal variable and compare it with rest of the elements so 89 will be compared against 2. Since 2 is less than 89 so replace value in MinVal by 2. Next compare this MinVal 2 against rest of the elements and replace value in MinVal wherever there is any value less than value in MinVal. So 2 will be compared against -13 and since -13 is less than 2 MinVal will be set to -13 and similarly this MinVal will be compared against 67 and 0 but none of them is les than -13, so MinVal will have -13 as final value. Now swap the two elements i:e the first element and the minimum element.

Since we started from first element so swap first element and the minimum element ie -13. So after first iteration the array will be like this

{-13, 2, 89, 67, 0};. The lowest element has taken the first place.

Repeat the same procedure for second element and then third element and so on.

Explainations of two for loops

for (int i=0; i<x.length; i++)   // Repeat for each element in the array

{

/*

Take each element from the array and set it to currentMin and store its index in minindex

*/

      int currentMin = x[i];

      int minIndex = i;

// Repeat for rest of the elements after i in the array

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

      {

          /*

                   Now compare this element i with the next elements of the array that’s why we choose y=i+1 because there is no fun of compare this element with itself

          */

          if (x[j] < currentMin)

         {

          // if the value of element I is less than any of the other elements in array then replace value in currentMin with the lower value. (In our case it will change from89 to 2 and 2 to -13 for first element)

          currentMin = x[j];         

          minIndex = j;

          }

      }

      // swap smallest element with element i

      if (minIndex != i)

      {

          // if the value of currentMin has changed then swap the two values (e:g 89 and -13)

          x[minIndex] = x[i];

          x[i] = currentMin;

      }

}