Bubble sort, Binary lookup method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.