I need help with a Java question so that it can output them described in the image below: import java.util.Scanner; public class LabProgram { public static void sortArray(int[] myArr, int arrSize) { for (int i = 0; i < arrSize - 1; i++) { for (int j = 0; j < arrSize - i - 1; j++) { if (myArr[j] < myArr[j + 1]) { // Swap elements if they are in the wrong order int temp = myArr[j]; myArr[j] = myArr[j + 1]; myArr[j + 1] = temp; } } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Read the number of elements in the array int arrSize = scanner.nextInt(); int[] myArr = new int[arrSize]; // Read and populate the array with elements for (int i = 0; i < arrSize; i++) { myArr[i] = scanner.nextInt(); } // Call the sorting method sortArray(myArr, arrSize); // Output the sorted array for (int i = 0; i < arrSize; i++) { System.out.print(myArr[i]); if (i < arrSize - 1) { System.out.print(","); } } System.out.println(); } }
I need help with a Java question so that it can output them described in the image below:
import java.util.Scanner;
public class LabProgram {
public static void sortArray(int[] myArr, int arrSize) {
for (int i = 0; i < arrSize - 1; i++) {
for (int j = 0; j < arrSize - i - 1; j++) {
if (myArr[j] < myArr[j + 1]) {
// Swap elements if they are in the wrong order
int temp = myArr[j];
myArr[j] = myArr[j + 1];
myArr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the number of elements in the array
int arrSize = scanner.nextInt();
int[] myArr = new int[arrSize];
// Read and populate the array with elements
for (int i = 0; i < arrSize; i++) {
myArr[i] = scanner.nextInt();
}
// Call the sorting method
sortArray(myArr, arrSize);
// Output the sorted array
for (int i = 0; i < arrSize; i++) {
System.out.print(myArr[i]);
if (i < arrSize - 1) {
System.out.print(",");
}
}
System.out.println();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images