//1. Implement a function that finds the specified value in an ordered integer array, finds the position where the value is returned, and cannot find the return-1.
Package demo; public class Mytest {public static void main (string[] args) {int[] arr={1,2,5,9,11,45}; int Index=findindext (ARR,0,ARR.L
ength-1,12);
System.out.println ("index=" +index); }//1.
Implements a function that finds the specified value in an ordered integer array, finds the position where the value is returned, and cannot find return-1. public static int Findindext (int[] arr,int left,int right,int ABC) {if (arr==null| | arr.length==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.
Implements a function that finds the specified value in an ordered integer array, finds the position where the value is returned, and cannot find return-1. Array Ascending imaginary group public int find (int[] array, int n) {if (array = = null) {return-1;} int len = Array.Length; 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) {returnMid }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 chain header public Node convert (int[] array) {if (AR Ray = NULL | |
Array.Length = = 0) {return null;}
Node head = new node ();
Head.data = array[0];
int len = Array.Length;
Node end = head;
for (int i=1; i< len. i++) {end = AddNode (end, array[i]); ///Add a node to the tail of the list nodes public node AddNode (node end, int data) {node node = new node (); node.data = data; end.next = node; retur
n node; }//3.
There are two arrays, [1,3,4,5,7,9] and [2,3,4,5,6,8], using the above function to generate two list Linka and/or linkb, and then merge the two lists into a linked list with the result [1,2,3,4,5,6,7,8,9].
Requirements: Do not generate a third linked list, do not generate a new node.
3.1 Use recursive implementation//Public Node comb (int[] Arraya, int[] arrayb) {Node Linka = convert (Arraya);
Node linkb = convert (Arrayb);
Node Head; if (Linka.data = = linkb.data) {head = Linka; Linka = linka.next; linkb = Linkb.next; head.next = NULL; }else if (Linka.data < linkb.data) {head = Linka; Linka = linka.next; head.next = null;}
else {head = linkb; linkb = linkb.next; head.next = null;}
Node end = head;
Comb (end, Heada, HEADB);
return head; //implement recursive public void comb (node end, node Heada, node headb) {if (Heada = = NULL && HEADB = null) {return;} else if (Heada = = null) {End.next = headb; return;}
else if (headb = null) {End.next = Heada; return;} if (Heada.data < headb.data) {end.next = Heada; heada = Heada.next;
end = End.next;
End.next = null;
Comb (end, Heada, HEADB); }else if (heada.data = = headb.data) {end.next = Heada; heada = heada.next; headb = headb.next; end = End.next; End.next = n
ull;
Comb (end, Heada, HEADB);
}else {end.next = headb; headb = headb.next; end = End.next; end.next = null; Comb (end, Heada, headb);}}
3.2 Implementation//loop implementation of the public Node comb (int[] Arraya, int[] arrayb) {//Convert link table Node Linka = convert (Arraya);
Node linkb = convert (Arrayb);
Gets the header node head; if (Linka.data = = LInkb.data) {head = Linka; Linka = linka.next; linkb = linkb.next; head.next = null;} else if (Linka.data < linkb.data) {head = Linka; Linka = linka.next; head.next = null;}
else {head = linkb; linkb = linkb.next; head.next = null;}
Node end = head; Add the smaller nodes in turn to the tail of the list while (Heada!= null && headb!= null) {if (Heada.data < headb.data) {end.next = Heada; Heada =
Heada.next;
end = End.next;
End.next = null; }else if (heada.data = = headb.data) {end.next = Heada; heada = heada.next; headb = headb.next; end = End.next; End.next = n
ull;
}else {end.next = headb; headb = headb.next; end = End.next; end.next = null;}} If one of the linked lists is empty, add another list directly to the tail of the synthetic list if (Heada = null) {End.next = headb;}
else if (headb = null) {End.next = Heada;} }
The above is a small set of Android on the recursive and binary algorithm example code, hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community website support!