"a" and "b" are two arrays. They both have "n" elements. What does the following algorithm do?
-
"a" and "b" are two arrays. They both have "n" elements. What does the following algorithm do? Try the following example if you need an example: a[0] = 2, a[1] = 1, a[2] = 3, b[0] = -2, b[1] = 42, b[2] = 22, n = 3. i <- 0 while (i < n) do j <- i+1 m <- a[i] while (j < n) do if (m > a[j]) then m <- a[j] end if j <- j + 1 end while b[i] <- m i <- i + 1 end while Answer: a. array b is an exact copy of array a b. array a and array b are not related c. array a becomes sorted, array b is not changed d. array b is the sorted version of array a e. none of the other descriptions is true Can anyone please tell me how to do this and help me find out the answer?
-
Answer:
Sometimes adding some indents will help figure out what's going on. In your algorithm, you have a lot of nested if and while statements, so space them out like this first to see how each is related to each other (I'm using periods because Yahoo won't show spaces): i <- 0 while (i < n) do ...j <- i+1 ...m <- a[i] ...while (j < n) do ......if (m > a[j]) then .........m <- a[j] ......end if ......j <- j + 1 ...end while ...b[i] <- m ...i <- i + 1 end while Your i is a counter that starts with 0, and your j is a counter that starts with 1. j will always be more than i. The "while (i < n) do" line limits this algorithm to 3 loops, since n is 3. Directly after this statement, j is established as a counter, and m is defined to be the i-th element of your matrix. The next while statement then compares m (the i-th element in the matrix) to the element after this (using j to read the next element). If m is larger, then the if statement makes m take on the smaller value. If m is smaller, nothing happens and j is indexed to the next value. Then the process repeats itself. If we get to the end of the matrix and m isn't replaced anymore, it becomes the i-th value in matrix B. After this, i is indexed to the next element in matrix A and the entire process is repeated once again, until all of matrix A has been checked. The result then is that through m, matrix A is sorted least to greatest and is written into matrix B, which is answer (d).
karenL01... at Yahoo! Answers Visit the source
Related Q & A:
- How to get all intersections between two simple polygons in O(n+k?Best solution by Computational Science
- How to merge two arrays of objects in angular?Best solution by Stack Overflow
- What is the difference between a B.S. and B.A. in psychology?Best solution by Yahoo! Answers
- What would the following characters from Aqua Teen Hunger Force do if?Best solution by Yahoo! Answers
- What to do with a b&B i had booked?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.