import java.util.Arrays;
//冒泡排序 public class Test { public static void main(String[] args) { int[] array = { 31, 22, 15, 77, 52, 32, 18, 25, 16, 7 }; // 冒泡 --> 两两比较 --> 提取出最大的数 在最后一位 //拿第一位和它后面的一位进行 两两比较 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 (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; //选择排序 public class Test { public static void main(String[] args) { int[] array = { 31, 22, 15, 77, 52, 32, 18, 25, 16, 7 }; // 选择 --> 第一位与后面所有的数字进行比较 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]) { // 如果 array[0]比array[1]大,交换位置 // 第三方变量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.Scanner; // 二分查找 折半查找法 从中间斩断,比较值。继续比较 public class Test11 { public static void main(String[] args) { // 二分查找法 插入排序 int[] array = { 3, 10, 15, 22, 33, 51, 77, 88 }; System.out.println(Arrays.toString(array)); Scanner input = new Scanner(System.in); do { System.out.print("输入需要插入的数据:"); int number = input.nextInt(); // 每次获取最左边的数和最右边的数 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); } //递归 public static int recursion(int[] array, int left, int right, int number) { // 如果大于右边,或者小于左边 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循环打印矩形 public class Rect { public void show(int width, int height) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (i == 0 || j == 0 || i == width - 1 || j == height - 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } }
//for循环打印菱形 public class Rhombus { public void show(int height) { int half = height / 2; for (int i = 1; i <= half; i++) { // 先打空格 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++) { // 先打空格 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(); } } }
//for循环打印等腰三角形 public class Isosceles { public void show(int height) { for (int i = 1; i <= height; i++) { // 先打空格 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循环打印之间三角形 public class Triangle { public void show(String str, int height) { for (int i = 0; i < height; i++) { for (int j = 0; j < i + 1; j++) { System.out.print(str); } System.out.println(); } } }
本文章地址http://www.vzeo.com/news/xuetang/800415.html 由 友站网 编辑整理,转载请注明出处