Question: public class Main { public static void main(String[] args) { int[] intArray = { 20, 35, -15, 7, 55, 1, -22 }; for (int lastUnsortedIndex

public class Main {

public static void main(String[] args) {

int[] intArray = { 20, 35, -15, 7, 55, 1, -22 };

for (int lastUnsortedIndex = intArray.length - 1; lastUnsortedIndex > 0; lastUnsortedIndex--) {

int largest = 0;

for (int i = 1; i <= lastUnsortedIndex; i++) { if (intArray[i] > intArray[largest]) { largest = i; } }

swap(intArray, largest, lastUnsortedIndex);

}

for (int i = 0; i < intArray.length; i++) { System.out.println(intArray[i]); }

}

public static void swap(int[] array, int i, int j) {

if (i == j) { return; }

int temp = array[i]; array[i] = array[j]; array[j] = temp;

}

}

please explain every line of the code and why on inner loop it start on 1 and <= instead of < and what does the code do ?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!