What does the following algorithm do? What is its time complexity?
```
public static int doSomething( int array[], int n ) {
int k = 0;
// find the largest value in the range array[0] to array[n-1]
for ( int i = 1; i < n; i++ )
if ( array[i] > array[k] )
k = i;
// postcondition: array[k] is the largest value in array
// Swap the largest value with the value in position 0
int temp = array[0];
array[0] = array[k];
array[k] = temp;
k = 1;
// find the largest value in the range array[1] to array[n-1]
for ( int i = 2; i < n; i++ )
if ( array[i] > array[k] )
k = i;
// postcondition: array[k] is the SECOND largest value in array
return array[k];
}
```
As the bolded comments inserted in the code above describe, the method finds and returns the second smallest value in the array. It also modifies the array in that the largest element will be found in position 0.
Its time complexity is O(n).
You might also like to view...
When editing someone else's comment, it is a good practice to:
A) contact the lead author and discuss any proposed changes to the comment. B) ask the person for his or her permission before making any changes to the comment. C) make the changes to the document and notify the person of your changes. D) add your name or initials to any changes that you make to the comment.
Tracer arrows show the relationship between active and ________ cells
Fill in the blank(s) with correct word
There are three basic categories of information that an Internet service provider can collect regarding its customers. Which of the following is not one of those categories?
a. Basic Subscriber Information b. Transactional History c. Financial Profile d. Customer generated content
Case 2Chuck was looking through the attic when he ran across an old black and white photo of his great-grandmother. He thought that the picture would look great hanging in the living room, and decided that it might look even better if he were to add some color to it. He had seen several examples of hand-colored photos in a photography book he had read recently, but was afraid to color the actual photo for fear of ruining it. Instead, he scanned the old photograph and used Photoshop to add the color. Where will Chuck find the tools he needs to add color to the photo?
A. In the Adjustments folder located in the Tools panel B. In the Hue / Saturation dialog box, found under Adjustments in the Tools panel C. It is not possible to colorize a black and white photo D. In the Hue / Saturation dialog box, located in Adjustments after clicking Image on the Menu bar