The basic idea of bubble sorting is to compare the values of adjacent elements, to exchange element values if conditions are met, to move the smaller elements to the front of the array, to move the large elements behind the array (that is, to swap the positions of two elements) so that the elements of the array rise from the bottom to the top as bubbles.
1 PackageCom.hanqi;2 3 Public classMaopao {4 5 Public Static voidMain (string[] args) {6 int[]array=New int[]{63,4,24,1,3,13};7SYSTEM.OUT.PRINTLN ("The process of bubble sorting is:");8 for(inti=1;i<array.length;i++)9 {Ten for(intj=0;j<array.length-i;j++) One { A if(array[i]>array[j+1]) - { - inttemp=Array[j]; theArray[j]=array[j+1]; -array[j+1]=temp; - } -System.out.print (array[j]+ ""); + } -System.out.print ("" "); + for(intj=array.length-i;j<array.length;j++) A { atSystem.out.print (array[j]+ ""); - } -System.out.print ("" "); - } - - } in -}
Two-Part search method
Assuming an ordered array, the intermediate element is first searched, the intermediate element subscript is recorded, and if the intermediate element is not the number to be looked up, the method is called recursively;
If the number to find is smaller than the middle element, then the left subscript is unchanged, the right subscript becomes the middle element subscript minus 1, and the right subscript is unchanged, the left subscript becomes the middle element subscript plus 1;
Specifies a recursive call to jump out of a condition. The right subscript large and left subscript when the recursive method is performed.
1 PackageCom.hanqi;2 3 Public classMaopao {4 5 Public Static voidMain (string[] args) {6intMiddelindex = (Leftindex + rightindex)/2; 7 intMiddelval =Arr[middelindex];8//conditions for recursive execution9if(Rightindex >=Leftindex)Ten { Oneif(Middelval >findnum) A { -Find (Leftindex, middelIndex-1, FindNum, arr); } - Elseif(Middelval <findnum) the { -Find (Middelindex + 1, Rightindex, FindNum, arr); } - Else - { +System.out.println (FindNum + "subscript in the array is:" +Middelindex); - } + } A Else at { -System.out.println ("The array does not exist:" +findnum); - } - } - PublicStaticvoidMain (string[] args) - { inintArr[] =Newint[] {3, 2, 6, 9, 2, 1, 5, 7, 22, 11, 78}; Find (0, Arr.length-1, 22, arr); -}
}
Bubble sort, Binary lookup method