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 }; /... ...
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(); } } }