Selection, Bubble sorting, binary search and flexible use of some for loops, flexible use of bubble

Source: Internet
Author: User

Selection, Bubble sorting, binary search and flexible use of some for loops, flexible use of bubble

Import java. util. Arrays;
// Bubble sort public class Test {public static void main (String [] args) {int [] array = {31, 22, 15, 77, 52, 32, 18, 25, 16, 7}; // bubble --> Two-to-two comparison --> extract the largest number in the last position
// Compare the first digit with the one next to it.
System. out. println (Arrays. toString (array); for (int j = 0; j <array. length; j ++) {for (int I = 0; I <array. length-1-j; I ++) {// if the preceding number is greater than the following number, the swap position if (array [I] <array [I + 1]) {int temp = array [I]; array [I] = array [I + 1]; array [I + 1] = temp ;}} System. out. println (Arrays. toString (array ));}}
Import java. util. arrays; // select the sort public class Test {public static void main (String [] args) {int [] array = {31, 22, 15, 77, 52, 32, 18, 25, 16, 7}; // select --> compare the first digit with all subsequent digits. System. out. println (Arrays. toString (array); for (int I = 0; I <array. length; I ++) {for (int j = I + 1; j <array. length; j ++) {if (array [I] <array [j]) {// if array [0] is larger than array [1, switch location // third-party variable temp int temp = array [I]; array [I] = array [j]; array [j] = temp ;}} System. out. println (Arrays. toString (array ));}}

 

Import java. util. Arrays; import java. util. Break; // The binary search method is used to cut off the middle and compare the values. Continue to compare public class Test11 {public static void main (String [] args) {// The binary lookup method inserts the sorted int [] array = {3, 10, 15, 22, 33, 51, 77, 88}; System. out. println (Arrays. toString (array); input = new partition (System. in); do {System. out. print ("input data to be inserted:"); int number = input. nextInt ();
// Obtain the leftmost number and rightmost number each time. int left = 0; int right = array. length-1; int result = recursion (array, left, right, number); System. out. println (result); int [] newArray = new int [array. length + 1]; for (int I = 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 int recursion (int [] array, int left, int right, int number) {// if the value is greater than the right side, or if (array [0]> number) return 0; else if (array [array. length-1] <number) return array. length; int center = (left + right)/2; if (left = right-1) return right; if (array [center]> number) {return recursion (array, left, center, number) ;}else {return recursion (array, center, right, number );}}}
// For Loop print the rectangle public class Rect {public void show (int width, int height) {for (int I = 0; I <width; I ++) {for (int j = 0; j 
// Print the diamond public class fig {public void show (int height) {int half = height/2; for (int I = 1; I <= half; I ++) {// enter a space for (int j = half-I + 1; j> 0; j --) {System. out. print ("") ;}for (int j = 0; j <2 * I-1; j ++) {System. out. print ("*");} System. out. println () ;}for (int I = 0; I <2 * half + 1; I ++) {System. out. print ("*");} System. out. println (); for (int I = 1; I <= half; I ++) {// enter a space for (int j = I; j> 0; j --) {System. out. print ("") ;}for (int j = 0; j <2 * (half-I) + 1; j ++) {System. out. print ("*");} System. out. println ();}}}
// Print the Isosceles public class Isosceles {public void show (int height) {for (int I = 1; I <= height; I ++) {// enter a space for (int j = height-I; j> 0; j --) {System. out. print ("") ;}for (int j = 0; j <2 * I-1; j ++) {System. out. print ("*");} System. out. println ();}}}
// For Loop printing public class Triangle {public void show (String str, int height) {for (int I = 0; I 

 

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.