Java basic instance
Print the isosceles triangle code
Public class ForForTest {
Public static void main (String [] args ){
For (int x = 0; x <5; x ++ ){
For (int y = x + 1; y <5; y ++ ){
System. out. print ("");
}
For (int z = 0; z
System. out. print ("*");
}
System. out. println ();
}
}
}
Half-lookup code:
// Exercise: Insert a value into the given array and ensure that the inserted array is arranged in a certain order.
Public class ArrayTest2 {// array lookup Value
Public static void main (String [] args ){
Int arr [] = {1, 2, 4, 5, 6, 7, 88, 9, 10 };
Int index = getIndex (arr, 8 );
System. out. println ("Number of keywords in the array" + (index + 1) + "bit ");
}
Public static int halfSeach_2 (int arr [], int key) {// the second half-way. Compare by index value
Int max, min, mid;
Min = 0;
Max = arr. length-1;
While (min <= max) {// compare when the condition is true
Mid = (max + min)/2; // The bitwise operation is equivalent to Division 2
If (key> arr [mid])
Min = mid + 1;
Else if (key <arr [mid])
Max = mid-1;
Else
Return mid;
}
Return-1;
}
// Obtain the nth digit of the value inserted into the array.
// Train of thought: Use semi-query. If a corresponding number exists in the array, this number is inserted to this position. If no number is found, the minimum bit of the semi-index is returned.
Public static int getIndex (int arr [], int key ){
Int max, min, mid;
Min = 0;
Max = arr. length-1;
Mid = (max + min)/2;
While (key! = Arr [mid]) {// when the key value is not equal to the median value, perform a half lookup.
If (key> arr [mid])
Min = mid + 1;
Else
Max = mid-1;
If (min> max)
Return min;
Mid = (max + min)/2;
}
Return mid;
}
/*
Semi-query: The premise is that the array is an ordered series.
1. First, define the maximum and minimum indexes of the array.
2. Calculate the intermediate index value
3. Compare the intermediate index value with the key value -----> if (key> arr [mid]) min = mid + 1; else if (key */
Public static int halfSeach (int arr [], int key ){
Int max, min, mid;
Min = 0;
Max = arr. length-1;
Mid = (max + min)/2;
While (key! = Arr [mid]) {// when the key value is not equal to the median value, perform a half lookup.
If (key> arr [mid])
Min = mid + 1;
Else
Max = mid-1;
If (min> max)
Return-1;
Mid = (max + min)/2; // always half in the cycle.
}
Return mid;
}
}
Sorting Algorithm code, including bubble, selection, and semi-order
Import java. util .*;
Public class ArrayTest {
Public static void main (String [] args ){
Int [] arr = {, 10 };
// Before sorting
PrintArray (arr );
// Sort
// SelectSort (arr );
BobleSort (arr );
// Arrays. sort (arr); // The encapsulated Sorting Algorithm in java
// After sorting
PrintArray (arr );
}
// Select sorting
Public static void selectSort (int [] arr ){
For (int x = 0; x for (int y = x + 1; y if (arr [x]> arr [y]) {
Swap (arr, x, y );
}
}
}
}
// Bubble Sorting: Compares adjacent elements each time to change positions that meet the conditions. After each layer of loops, the maximum value returns to the top of the element. Therefore, when compared, the value of the inner loop array is reduced by one.
Public static void bobleSort (int arr []) {
For (int x = 0; x for (int y = 0; y if (arr [y]> arr [y + 1]) {
Swap (arr, y, y + 1 );
}
}
}
}
// Exchange Element
Public static void swap (int arr [], int a, int B ){
Int temp = arr [a];
Arr [a] = arr [B];
Arr [B] = temp;
}
// Print the Array
Public static void printArray (int arr []) {
System. out. print ("[");
For (int I = 0; I if (I! = Arr. length-1 ){
System. out. print (arr [I] + ",");
} Else {
System. out. print (arr [I]);
}
}
System. out. print ("]");
System. out. println ();
}
}