SoFunction
Updated on 2025-03-11

Algorithm instance code on recursion and dichotomy in Android

// 1. Implement a function to find the specified value in a ordered integer array in binary search. If it is found, it returns the position of the value, and -1 cannot be found.

package demo;
public class Mytest {
public static void main(String[] args) {
int[] arr={1,2,5,9,11,45};
int index=findIndext(arr,0,-1,12);
("index="+index);
}
// 1. Implement a function to find the specified value in a ordered integer array in binary search. If it is found, it returns the position of the value, and -1 cannot be found.public static int findIndext(int[] arr,int left,int right,int abc){
if(arr==null||==0){
return -1;
}
if(left==right){
if(arr[left]!=abc){
return -1;
}
}
int mid=left+(right-left)/2;//3//4
if(arr[mid]>abc){//
right=mid-1;
return findIndext(arr,left,right,abc);
}else if(arr[mid]<abc){//5<45//9<45/11<45
left=mid+1;
return findIndext(arr,left,right,abc);//2,5//3,5//4.5
}else{
return mid;
}
}
}
/ 1. Implement a function,Find the specified value in a ordered array of integers,Find the location of the value.,Can't find return -1。
// array to raise virtual arraypublic int find(int[] array, int n){
if(array == null){
return -1;
}
int len = ;
if(n < array[0] || n > array[len-1]){
return -1;
}
int left = 0;
int right = len -1;
while(left < right){
int mid = left + (right - left) / 2;
if(array[mid] == n){
return mid;
}else if(array[mid] < n){
left = mid + 1;
}else{
right = mid - 1;
}
}
if (array[left] == n){
return left;
} else {
return right;
}
}
// 2. Write a function to convert an array into a linked list.// Requirements: Do not use library functions (such as List, etc.)public static class Node{
Node next;
int data;
}
// Return to the header of the linked listpublic Node convert(int[] array){
if(array == null ||  == 0){
return null;
}
Node head = new Node();
 = array[0];
int len = ;
Node end = head;
for(int i=1; i< len ; i++){
end = addNode(end, array[i]);
}
return head;
}
// Add a node to the end of the linked listpublic Node addNode(Node end, int data){
Node node = new Node();
 = data;
 = node;
return node;
}
// 3. There are two arrays, [1,3,4,5,7,9] and [2,3,4,5,6,8], and use the above function to generate two linked lists linkA and// linkB, then merge the two linked lists into one linked list, and the result is [1,2,3,4,5,6,7,8,9].// Requirements: Do not generate a third linked list, do not generate new nodes.// 3.1 Implementation using recursive// 
public Node comb(int[] arrayA, int[] arrayB){
Node linkA = convert(arrayA);
Node linkB = convert(arrayB);
Node head;
if( == ){
head = linkA;
linkA = ;
linkB = ;
 = null;
}else if ( < ){
head = linkA;
linkA = ;
 = null;
}else {
head = linkB;
linkB = ;
 = null;
}
Node end = head;
comb(end, headA, headB);
return head;
}
// Implement recursionpublic void comb(Node end, Node headA, Node headB){
if(headA == null && headB == null){
return;
}else if(headA == null){
 = headB;
return;
}else if(headB == null){
 = headA;
return;
}
if( < ){
 = headA;
headA = ;
end = ;
 = null;
comb(end, headA, headB);
}else if( == ){
 = headA;
headA = ;
headB = ;
end = ;
 = null;
comb(end, headA, headB);
}else {
 = headB;
headB = ;
end = ;
 = null;
comb(end, headA, headB);
}
}

// 3.2 Implementation using loop// Loop implementationpublic Node comb(int[] arrayA, int[] arrayB){
// Convert link listNode linkA = convert(arrayA);
Node linkB = convert(arrayB);
// Get the header nodeNode head;
if( == ){
head = linkA;
linkA = ;
linkB = ;
 = null;
}else if ( < ){
head = linkA;
linkA = ;
 = null;
}else {
head = linkB;
linkB = ;
 = null;
}
Node end = head;
// Add smaller nodes to the end of the linked list in turnwhile(headA != null && headB != null){
if( < ){
 = headA;
headA = ;
end = ;
 = null;
}else if( == ){
 = headA;
headA = ;
headB = ;
end = ;
 = null;
}else {
 = headB;
headB = ;
end = ;
 = null;
}
}
// If one of the linked lists is empty, add the other linked list directly to the end of the synthetic linked listif(headA == null){
 = headB;
}else if(headB == null){
 = headA;
}
return head;
}

The above is the algorithm example code for recursion and dichotomy in Android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!