Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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--) { for (int i = 0; i < lastUnsortedIndex; i++) { if (intArray[i] > intArray[i + 1]) { swap(intArray, i, i + 1); } } }

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, i am new to java. also on the for loop part why + 1 and - 1. and what does this program do ?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions

Question

What does the balance sheet report?

Answered: 1 week ago

Question

explain what is meant by the terms unitarism and pluralism

Answered: 1 week ago