Write a method to search an array of integers for a given integer.
Unlike the search methods of our Dictionary Class, this will be a static method.
static private boolean search (int x, int a [])
{
for (int i = 0;
i < a.length;
i + +)
{
if (a [i]
==
x)
{
return true;
}
return false;
}
Here is a program which will sort an array of integers from smallest value to highest value:
for (int i = 0;
i < a.length;
i + +)
{
for (int s = i + 1;
s <
a.length;
s + +)
{
if (a [i] > a [s])
{
int temp = a [i];
a [i] = a [s];
a [s] = temp;
}
}
} |