1. array Inversion
CopyCode The Code is as follows: Import java. util. arrays;
Public class arrayreverse {
Public static void main (string [] ARGs ){
Int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Reverse (ARR );
}
Public static void reverse (INT [] ARR ){
For (INT I = 0; I <arr. Length/2; I ++ ){
Int temp = arr [I];
Arr [I] = arr [arr. length-1-i];
Arr [arr. length-1-i] = temp;
}
System. Out. println (arrays. tostring (ARR ));
}
}
2. Evaluate the most value
Copy codeThe Code is as follows: Class zuizhi
{
Public static void main (string [] ARGs)
{
Int [] A = {2, 92, 34, 54, 28 };
Minmethod ();
Maxmethod ();
}
Public static void minmethod (INT [])
{
Int min = A [0]; // The array cannot be initialized as 0 and may have negative numbers.
For (INT I = 0; I <A. length; I ++)
{
If (A [I] <min)
Min = A [I];
}
System. Out. println ("the smallest element in the array is:" + min );
}
Public static void maxmethod (INT [])
{
Int max = A [0]; // The array cannot be initialized as 0 and may have negative numbers.
For (INT I = 0; I <A. length; I ++)
{
If (A [I]> MAX)
Max = A [I];
}
System. Out. println ("the largest element in the array is:" + max );
}
}
3. half-lookup
copy Code the code is as follows:/*
half-lookup:
3 5 7 9 10 14
min = 0 max = length-1 MID = (MAX + min) /2
*/
class binarysearch
{< br> Public static void main (string [] ARGs)
{< br> int [] A = {3, 5, 7, 9, 10, 14 };< br> int Index = binarysearch_1 (A, 7);
system. out. println (INDEX);
Index = binarysearch_2 (A, 14);
System. out. println (INDEX);
}< br> Public static int binarysearch_1 (INT [] A, int key)
{< br> int min = 0, mid, max =. length-1;
mid = (min + max)/2;
while (key! = A [Mid])
{< br> if (a [Mid]> key)
{< br> max = mid-1;
}< br> else if (a [Mid]
{< br> min = Mid + 1;
}< br> If (max
return-1;
mid = (min + max)/2;
}
Return mid;
}
Public static int binarysearch_2 (INT [] A, int key)
{
Int min = 0, max = A. Length-1, mid;
While (Min <= max)
{
Mid = (MAX + min)> 1;
If (A [Mid]> key)
{
Max = mid-1;
}
Else if (a [Mid] <key)
{
Min = Mid + 1;
}
Else
Return mid;
}
Return-1;
}
}