Java Reference
In-Depth Information
if
(list[loc] >= searchItem)
{
found =
true
;
break
;
}
if
(found)
if
(list[loc] == searchItem)
return
loc;
else
return
-1;
else
return
-1;
}
b. i. 5 ii. 7 iii. 8 iv.11
5. List before the first iteration:
26, 45, 17, 65, 33, 55, 12, 18
List after the first iteration:
12, 45, 17, 65, 33, 55, 26, 18
List after the second iteration:
12, 17, 45, 65, 33, 55, 26, 18
List after the third iteration:
12, 17, 18, 65, 33, 55, 26, 45
List after the fourth iteration:
12, 17, 18, 26, 33, 55, 65, 45
List after the fifth iteration:
12, 17, 18, 26, 33, 55, 65, 45
List after the sixth iteration:
12, 17, 18, 26, 33, 45, 65, 55
List after the seventh iteration:
12, 17, 18, 26, 33, 45, 55, 65
7.
3
9.
10, 12, 18, 21, 25, 28, 30, 71, 32, 58, 15
11.
Selection sort: 49,995,000 comparisons; insertion sort: 25,007,499 comparisons
13.
public
static
void
descendingToAscending(
int
[]
list,
int
length)
{
int
temp;
int
index;
int
last = length - 1;
for
(index = 0; index <= (length - 1) / 2; index++)
{
temp = list[index];
list[index] = list[last];
list[last] = temp;
last- -;
}
}