King - 3 months ago 49

Java Question

I am having trouble understanding this pseudocode, and implementing it into my program. Can anybody explain it better or show me how the code would look? Thanks.

`A - an array containing the list of numbers`

numItems - the number of numbers in the list

for i = 0 to numItems - 1

for j = i+1 to numItems

if A[i] > A[j]

// Swap the entries

Temp = A[i]

A[i] = A[j]

A[j] = Temp

End If

Next j

Next i

Answer

Well, let's translate the pseudo-code to pseudo-English.

```
A - an array containing the list of numbers
numItems - the number of numbers in the list
for i = 0 to numItems - 1
for j = i+1 to numItems
if A[i] > A[j]
// Swap the entries
Temp = A[i]
A[i] = A[j]
A[j] = Temp
End If
Next j
Next i
```

which might read

```
Count through each item, from the beginning to the end, calling it X
While considering item X, count through each item after it, from just
after X to the end, calling it Y
If X is bigger than Y, swap the two, temporarily storing X in Temp
so it doesn't get lost when we copy Y into X. Copy Y into X, and then
Copy the temporarily stored old value of X (remember it is in Temp)
back into Y. Now the values of X and Y are swapped (so X is now smaller
than Y)
```

Now it's your job to write it in code.