1. Dichotomy
Int[] arr = {1,2,3,4,5,6,7,8,9,10};
int key = 2;
int min, Max, mid;
min = 0;
max = arr.length-1;
Mid = (Min+max) >>1;//(Min+max)/2
while (Arr[mid]!=key) {
if (Key>arr[mid]) {
min = mid+1;
}else if (Key<arr[mid]) {
max = mid-1;
}
if (max<min) {
Return
}
Mid = (Min+max) >>1;//(Min+max)/2
}
System.out.println (mid);
2. Bubble sort
Int[] arr1 = {21,14,56,2,5,16,27,83,9,10};
int common;
for (int i=0;i<arr1.length;i++) {
for (int j=i+1;j<arr1.length;j++) {
if (Arr1[i]<arr1[j]) {
common = arr1[i];
Arr1[i] = Arr1[j];
ARR1[J] = common;
}
}
}
System.out.println (arrays.tostring (arr1));
3, beg greatest common divisor, least common multiple
System.out.println ("Please enter the first number:");
Scanner sc=new Scanner (system.in);
int a = Sc.nextint ();
System.out.println ("Please enter a second number:");
int b = Sc.nextint ();
int C;
int mul = 1;
int num = 1;
if (a>b) {
C=a;
A=b;
B=c;
}
for (int i=2;i<a;i++) {
if (a%i==0&&b%i==0) {
a=a/i;
b=b/i;
Mul*=i;
num = i;
}
}
SYSTEM.OUT.PRINTLN ("Least common Multiple:" +a*b*mul);
System.out.println ("Greatest common divisor:" +num);
Little algorithmic problem, no place to record, it's here.