A classmate of yours has coded the following version of selectionSort().Will it work? Explain.

```
/**
* Sort the array elements in ascending order
* using selection sort.
* @param a the array of Objects to sort
* @throws NullPointerException if a is null
*/
static void selectionSort( Object[] a ) {
if ( a == null )
throw new NullPointerException();
// while the size of the unsorted part is > 1
for ( int unsortedSize = a.length; unsortedSize > 1; unsortedSize-- ) {
// find the position of the largest
// element in the unsorted section
int maxPos = 0;
for ( int pos = 1; pos < unsortedSize; pos++ )
if ( a[pos] > a[maxPos] )
maxPos = pos;
// postcondition: maxPos is the position
// of the largest element in the unsorted
// part of the array
// Swap largest value with the last value
// in the unsorted part
Object temp = a[unsortedSize – 1];
a[unsortedSize – 1] = a[maxPos];
a[maxPos] = temp;
}
}

```

No, the comparison of objects in the if-statement is comparing the object references, not the object values.

Computer Science & Information Technology

You might also like to view...

Font size is measured in ________

A) centimeters B) pixels C) points D) millimeters

Computer Science & Information Technology

In the original Caesar Cipher, each letter in the message was shifted forward ____ positions.

A. two B. three C. four D. five

Computer Science & Information Technology