selection, bubble sorting, binary lookup, and some flexible use for loops

Source: Internet
Author: User

Importjava.util.Arrays;
//bubble Sort public classTest { public Static voidmain (string[] Args) {int[] array = {31, 22, 15, 77, 52, 32, 18, 25, 16, 7 }; //bubbles--22 comparison--extracts The largest number in the last one
Take the first and the one behind it. 22 comparison
System.out.println (arrays.tostring (array)); for(intj = 0; J < array.length; J + +) { for(inti = 0; I < array.length-1-j; i++) { //If the previous number is greater than the number of subsequent exchange positions if(array[i] < Array[i + 1]) { inttemp =array[i]; array[i]= Array[i + 1]; Array[i+ 1] =temp; }}} System.out.println (arrays.tostring (array)); }}
Importjava.util.Arrays; //select Sort  public classTest { public Static voidmain (string[] Args) {int[] array = {31, 22, 15, 77, 52, 32, 18, 25, 16, 7 }; //Select the first digit to compare with all subsequent numbersSystem.out.println (arrays.tostring (array));  for(inti = 0; I < array.length; i++) {             for(intj = i + 1; J < array.length; J + +) {                if(array[i] <Array[j]) {                    //if array[0] is larger than array[1], swap position//third-party Variable Temp                    inttemp =array[i]; array[i]=array[j]; array[j]=temp;    }}} System.out.println (arrays.tostring (array)); }}

Importjava.util.Arrays;Importjava.util.Scanner;//binary Find binary Find method cut from the middle, compare Values. Continue to compare public classTest11 { public Static voidmain (string[] Args) {//Binary Lookup method insert sort        int[] array = {3, 10, 15, 22, 33, 51, 77, 88 };        System.out.println (arrays.tostring (array)); Scanner input=NewScanner (system.in);  do{System.out.print ("enter the data you want to insert:"); intNumber =Input.nextint ();
//gets the leftmost number and the rightmost number each time . intleft = 0; intright = Array.length-1; intresult =recursion (array, left, right, number); System.out.println (result); int[] NewArray =New int[array.length + 1]; for(inti = 0; I < newarray.length; i++) { if(i <Result) {newarray[i]=array[i]; } Else if(i = =Result) {newarray[i]=number ; } Else{newarray[i]= Array[i-1]; }} Array=newarray; System.out.println (arrays.tostring (array)); } while(true); } //Recursive public Static intRecursion (int[] array,intLeftintRightintnumber ) { //if greater than right, or less than left if(array[0] >number )return0; Else if(array[array.length-1] <number )returnarray.length; intCenter = (left + RIGHT)/2; if(left = = Right-1) returnright ; if(array[center] >number ) { returnrecursion (array, left, center, number); } Else { returnrecursion (array, center, right, number); } }}
//for Loop Print Rectangle public classRect { public voidShowintWidthintHeight) {         for(inti = 0; I < width; i++) {             for(intj = 0; J < height; J + +) {                if(i = = 0 | | j = 0 | | i = = WIDTH-1 | | j = height-1) System.out.print ("*"); ElseSystem.out.print (" ");        } System.out.println (); }    }}
//for Loop Print Diamond public classRhombus { public voidShowintHeight) {        intHalf = HEIGHT/2;  for(inti = 1; I <= half; i++) {            //make a space first             for(intj = half-i + 1; J > 0; j--) {System.out.print (" "); }             for(intj = 0; J < 2 * i-1; J + +) {System.out.print ("*");        } System.out.println (); }         for(inti = 0; I < 2 * half + 1; i++) {System.out.print ("*");        } System.out.println ();  for(inti = 1; I <= half; i++) {            //make a space first             for(intj = i; J > 0; j--) {System.out.print (" "); }             for(intj = 0; J < 2 * (half-i) + 1; J + +) {System.out.print ("*");        } System.out.println (); }    }}
//for Loop printing isosceles triangle public classisosceles { public voidShowintHeight) {         for(inti = 1; I <= height; i++) {            //make a space first             for(intj = height-i; J > 0; j--) {System.out.print (" "); }             for(intj = 0; J < 2 * i-1; J + +) {System.out.print ("*");        } System.out.println (); }    }}
// for loop between printing triangles  public class Triangle {    publicvoidint  height] {        for (int i = 0; I < height; i++) {            for (int j = 0; J < i + 1; J + + ) {                System.out.print (str);            }            System.out.println ();     }}}

selection, bubble sorting, binary lookup, and some flexible use for loops

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.