Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For lines 32 - 49 and you type what is going on when the input is 5 10 4 39 12 2 for the nested

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedFor lines 32 - 49 and you type what is going on when the input is 5 10 4 39 12 2 for the nested for loop, and if it is way too much to type, then can you explain to me why this method was used to sort the numbers from least to greatest. I just want to understand why this specific method works for sorting number I cannot get my head around this part.

yava 10* 2 Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). 3 The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. 4 5 Ex: If the input is: 6 7 5 10 4 39 12 2 8 the output is: 9 10 2 4 10 12 39 11 For coding simplicity, follow every output value by a space, including the last one. 12 13 Your program must define and call the following method. When the sortArray() method is complete, the array passed in as the 14 parameter should be sorted. 15 16 public static void sortArray (int[] myArr, int arrSize) 17 18 Hint: There are many ways to sort an array. You are welcome to look up and use any existing algorithm. Some believe the simplest 19 to code is bubble sort: https://en.wikipedia.org/wiki/Bubble_sort. But you are welcome to try others: 20 https://en.wikipedia.org/wiki/Sorting_algorithm. 21 */ 22 23 package methodsContinued; 24 25 import java.util.Scanner; 26 27 public class SortAnArray { 280 public static void sortArray (int[] myArr, int arrSize) { // myArr = the values stored in indexes, 29 // arrSize = the array size (in elements) 30 int tempValue; 31 32 /* 33 * What this nested for loop is doing is this: 5 10 4 39 12 2 i = 0, j = 0 35 if (myArr[0] > myArr[ + 1]) { 36 tempValue = myArr[1]; X E l B5 A Problems @ Javadoc Declaration Console X SortAnArray (Java Application] C:\Program FilesVava\jdk-14.0.1\bin\javaw.exe (Jan 6, 2021, 3:09:21 PM3:09:24 PM) 5 10 4 39 12 2 2 4 10 12 39 Writable Smart Insert 50:28: 1874 28 SortAnArray.java X 23 package methodsContinued; 24 25 import java.util.Scanner; 26 27 public class SortAnArray { public static void sortArray(int[] myArr, int arrSize) { // myArr = the values stored in indexes, 29 // arrSize = the array size (in elements) 30 int tempValue; 31 32 /* 33 * What this nested for loop is doing is this: 5 10 4 39 12 2 34 i = 0, j = 0 35 if (myArr[0] > myArr[ 0 + 1]) { 36 tempValue = myArr[1]; 37 myArr[1] = myArr[ 0 + 1]; 38 myArr[0 + 1] = tempValue; 39 if (10 > 4) { 40 tempValue = 4; 41 myArr[1] = 4; 42 myArr[1] = 4; 43 44 i = 0 j = 1 45 if (myArr[1] > myArr[1 + 1]) { 46 tempValue = myArr[1]; 47 myArr[1] = myArr[1 + 1]; 48 myArr[1 + 1] = tempValue; 49 if (4 > 39) { 50 51 52 */ for (int i = 0; i myArr[j + 1]) { tempValue = myArr[j]; 57 myArr[j] = myArr(j + 1]; 58 myArr(j + 1] = tempValue; Problems @ Javadoc Declaration Console X terminated> SortAnArray [Java Application] C:\Program Files\Java\jdk-14.0.1\bin javaw.exe (Jan 6, 2021, 3:09:21 PM3:09:24 PM) 5 10 4 39 12 2 24 10 12 39 Writable Smart Insert 50:28: 1874 */ for (int i = 0; i myArr[j + 1]) { tempValue = myArr[j]; myArr[j] = myArr[j + 1]; myArr(j + 1] = tempValue; } D'SortAnArray.java X 52 53 54 55 56 57 58 59 60 61 62 63 640 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 89 public static void main(String[] args) { // This is the main method so the program will begin reading here. Scanner scnr = new Scanner(System.in); int[] uservals = new int[scnr.nextInt()]; // The user will input the number of elements and saved into uservals. for (int i = 0; i SortAnArray [Java Application] C:\Program Files\Java\jdk-14.0.1\bin\javaw.exe (Jan 6, 2021, 3:09:21 PM -3:09:24 PM) 5 10 4 39 12 2 2 4 10 12 39 Writable Smart Insert 50:28: 1874 } 61 62 63 640 65 66 67 public static void main(String[] args) { // This is the main method so the program will begin reading here. Scanner scnr = new Scanner(System.in); int[] uservals = new int[scnr.nextInt()]; // The user will input the number of elements and saved into uservals. for (int i = 0; i SortAnArray [Java Application] C:\Program Files\Java\jdk-14.0.1\bin\javaw.exe (Jan 6, 2021, 3:09:21 PM 3:09:24 PM) 5 10 4 39 12 2 2 4 10 12 39 Writable Smart Insert 50 : 28: 1874

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

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions

Question

Use MATLAB to compute the mean for each column in matrix B

Answered: 1 week ago