Print 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<x;z++) {
System.out.print ("*");
}
System.out.println ();
}
}
}
Binary Find code:
Exercise: Give a numeric value, require insertion into the given array, and ensure that the array is sorted in a certain order after insertion
public class arraytest2{//Array lookup values
public static void Main (String []args) {
int arr[] = {1,2,3,4,5,6,7,88,9,10};
int index = GetIndex (arr,8);
SYSTEM.OUT.PRINTLN ("The keyword exists in the array of the" + (index+1) + "bit");
}
public static int halfseach_2 (int arr[],int key) {//second binary method. Compare by index value
int max,min,mid;
min = 0;
max = arr.length-1;
while (Min<=max) {//The condition is true, compare
Mid = (max+min)/2; The bitwise operation is equivalent to 2
if (Key>arr[mid])
min = mid +1;
else if (Key < Arr[mid])
max = mid-1;
Else
return mid;
}
return-1;
}
The number of digits inserted into the array.
Idea: Using binary lookup, if you find the corresponding number in the array, then insert that number into that position, if not found then returns the smallest bit of the binary index.
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 critical value is not equal to the middle value, the binary lookup is performed.
if (Key>arr[mid])
min = mid +1;
Else
max = mid-1;
if (min > Max)
return min;
Mid = (max + min)/2;
}
return mid;
}
/*
Binary lookup: If the array is an orderly sequence
1. First define the maximum index and minimum index of the array
2. Calculate the intermediate index value
3, compare the corresponding median index value with the key value-----> if (key>arr[mid]) min = Mid+1;else if (key<arr[mid]) max = mid-1;
*/
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 critical value is not equal to the middle value, the binary lookup is performed.
if (Key>arr[mid])
min = mid +1;
Else
max = mid-1;
if (min > Max)
return-1;
Mid = (max + min)/2; Binary is always carried out in the loop.
}
return mid;
}
}
Code for sorting algorithms, including bubbling, selection, binary sorting
Import java.util.*;
public class arraytest{
public static void Main (String []args) {
int []arr = {23,3,1,53,43,13,10};
Before sorting
PrintArray (arr);
Sort
Selectsort (arr);
Boblesort (arr);
Arrays.sort (arr); Sorted algorithms that have been packaged in Java
After sorting
PrintArray (arr);
}
Select sort
public static void Selectsort (int []arr) {
for (int x=0;x<arr.length-1;x++) {
for (int y=x+1;y<arr.length;y++) {
if (Arr[x]>arr[y]) {
Swap (arr,x,y);
}
}
}
}
Bubble sort: Each time the adjacent element is compared, the transposition of the condition is met. After each layer loop, the maximum value goes back to the top of the element, so the value of the inner loop array is reduced by one bit in comparison.
public static void Boblesort (int arr[]) {
for (int x=0;x<arr.length-1;x++) {
The reason for (int y=0;y<arr.length-x-1;y++) {//minus 1 is to prevent the array from overstepping.
if (Arr[y]>arr[y+1]) {
Swap (arr,y,y+1);
}
}
}
}
Interchange element
public static void swap (int arr[],int A, int b) {
int temp = Arr[a];
Arr[a] = arr[b];
ARR[B] = temp;
}
Print array
public static void PrintArray (int arr[]) {
System.out.print ("[");
for (int i=0;i<arr.length;i++) {
if (i! = arr.length-1) {
System.out.print (arr[i]+ ",");
}else{
System.out.print (Arr[i]);
}
}
System.out.print ("]");
System.out.println ();
}
}
Java Foundation Instance