浅谈选择、冒泡排序,二分查找法以及一些for循环的灵活运用
程序员文章站
2023-12-17 09:17:40
如下所示:
import java.util.arrays;
//冒泡排序
public class test {
public static vo...
如下所示:
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(); } } }
以上这篇浅谈选择、冒泡排序,二分查找法以及一些for循环的灵活运用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。