冒泡排序:
public static void main(String[] args) { int []arr={11,23,45,32,1,98,19,68,74}; System.out.println("排序前数据:"); print(arr); System.out.println(); System.out.println("冒泡排序后:"); int[] arr1=printmaopao(arr); print(arr1); } public static void print(int [] arr){ for (int i=0;i< arr.length;i++){ if (i==0){ System.out.print("["+arr[i]+","); }else if (i== arr.length-1){ System.out.print(arr[i]+"]"); }else { System.out.print(arr[i]+","); } } } public static int[] printmaopao(int arr[]){ for (int i=0;i< arr.length-1;i++){//外层循环实现排序次数每次循环完一次后最后一位数一定是最大的 for (int j=0;j< arr.length-1-i;j++) { if (arr[j]>arr[j+1]){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } return arr; } } 结果:
排序前数据:
[11,23,45,32,1,98,19,68,74]
冒泡排序后:
[1,11,19,23,32,45,68,74,98]
二分查找:
public class Days7 { public static void main(String[] args) { int[] arr = {11, 23, 45, 32, 1, 98, 19, 68, 74}; System.out.println("排序前数据:"); print(arr); System.out.println(); System.out.println("冒泡排序后:"); int[] arr1 = printmaopao(arr); print(arr1); System.out.println("请输入要查找的数据:"); int[] arr2 = printchazhao(arr1); print(arr2); } public static void print(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i == 0) { System.out.print("[" + arr[i] + ","); } else if (i == arr.length - 1) { System.out.print(arr[i] + "]"); } else { System.out.print(arr[i] + ","); } } } public static int[] printmaopao(int arr[]) { for (int i = 0; i < arr.length - 1; i++) {//外层循环实现排序次数每次循环完一次后最后一位数一定是最大的 for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } public static int[] printchazhao(int arr1[]) { Scanner wy = new Scanner(System.in); System.out.println("请输入要查找的数据:"); int number = wy.nextInt(); int start = 0; int end = arr1.length - 1; int mid = (start + end) / 2; while (start <= end) { if (number == arr1[mid]) { System.out.println("找到元素" + number + "下标索引为" + mid); break; } else if (number < arr1[mid]) { end = mid - 1; mid = (start + end) / 2; } else if (number < arr1[mid]) { start = mid + 1; mid = (start + end) / 2; } else { System.out.println("没有查询到数据:"); } } return arr1; } } 结果:
排序前数据:
[11,23,45,32,1,98,19,68,74]
冒泡排序后:
[1,11,19,23,32,45,68,74,98]请输入要查找的数据:
请输入要查找的数据:
32
找到元素32下标索引为4
[1,11,19,23,32,45,68,74,98]
杨辉三角:
public static void main(String[] args) { int[][] arr2 = printyanghui(); print(arr2); } public static int[][] printyanghui() { Scanner wy = new Scanner(System.in); System.out.println("请输入行数:"); int n = wy.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < arr.length; i++) {//定义每一排第一个和最后一数都为1 arr[i][0] = 1; arr[i][i] = 1; } for (int i = 2; i < arr.length; i++) {//这里定义2因为前面定义了每一排第一个数和最后一个都为1第一排和第二排都是1 for (int j = 1; j < arr[i].length; j++) {//这里j等于1因为前面定义了第一个和最后一个元素为1 arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } } return arr; } public static void print ( int arr[][]){//输出语句和九九乘法表类似 for (int i = 0; i < arr.length; i++) { for (int j = 0; j <=i; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } }
输出
请输入行数:
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1