java基础代码(冒泡排序、二分查找、数组动态扩容、杨辉三角)

这些代码都是一些基础代码,多练习,熟能生巧

冒泡排序

public class BubbleSorting{
    public static void main(String[] args){
        int[]arr2= {43,5634,42,66,24};
        int t;
        for (int j = 0; j < arr2.length;j++ ){
            for (int i = 0;i < arr2.length-1-j;i++){
                if (arr2[i] > arr2[i+1]){
                    t = arr2[i];
                    arr2[i] = arr2[i+1];
                    arr2[i+1] = t ;
                }
            }
        }
        for(int i = 0;i < arr2.length;i++){
            System.out.print(arr2[i]+" ");
        }
    }
}

二分查找

public class BinarySearch{
    public static void main(String[] args){
        Scanner myscanner = new Scanner(System.in);
        int []arr = {1,2,3,4,5,6};
        int beg=0,end= arr.length-1;
        System.out.println("请输入需要查找的数字,你将得到这个数字所在的位置");
        int sum = myscanner.nextInt();
        while (sum != arr[(beg+end)/2]){
            if(sum > arr[(beg+end)/2]){
                beg = (beg+end)/2;
            }else if(sum < arr[(beg+end)/2]){
                end = (beg+end)/2;
            }
        }
        int cnt = 1+(beg+end)/2;
        System.out.println("你要查找的数字是" + sum
                + "它在第" + cnt + "个");
    }
}

数组动态扩容

public class addarray{
    public static void main(String[] arg){
        int []arr1 = {1,2,3};
        char ch;
        //int cnt = arr1.length-1;//控制给对应数组赋值
        Scanner myscanner = new Scanner(System.in);
        do{
            int []arr2 = new int [arr1.length+1];//开辟新空间,每次循环比arr1多1个空间
            for (int j = 0;j < arr2.length-1;j++){
                arr2[j] = arr1[j];//将之前就有的数拷贝到arr2
                System.out.print(arr2[j] + " ");//输出原有数据
            }
            //扩容
            System.out.println("\n输入需要扩容的数字");
            arr2[arr1.length] = myscanner.nextInt();
            for (int i = 0;i < arr2.length;i++){
                System.out.print(arr2[i] + " ");
            }
            System.out.println("是否继续扩容");
            ch = myscanner.next().charAt(0);
            arr1 = arr2;//第61行需要new新的数组空间,将之前的旧空间替换成每一次循环生成的新空间。
          //  cnt++;
        } while(ch == 'y');
        System.out.println("已经退出程序");
    }
}

杨辉三角

public class YHTriangle{
    public static void main(String[] args) {
        int[][] yanghsj;
        int num;
        Scanner myscanner = new Scanner(System.in);
        System.out.println("请输入想要打印杨辉三角的层数:n");
        num = myscanner.nextInt();
        yanghsj = new int[num][];
        for (int i = 0; i < yanghsj.length; i++) {
            yanghsj[i] = new int[i + 1];
            for (int j = 0; j < yanghsj[i].length; j++) {

                if (j == 0 || j == yanghsj[i].length - 1) {
                    yanghsj[i][j] = 1;
                } else {
                    yanghsj[i][j] = yanghsj[i - 1][j] + yanghsj[i - 1][j - 1];
                }
            }
        }
        for (int i = 0; i < yanghsj.length; i++) {
            for (int k = yanghsj.length; k > i + 1; k--) {
                System.out.print(" ");
            }
            for (int j = 0; j < yanghsj[i].length; j++) {
                System.out.print(yanghsj[i][j] + " ");
            }
            System.out.print("\n");
        }
    }
}

你可能感兴趣的:(java,基础,java,排序算法,算法)