算法-排序

来源:互联网 发布:微信恶搞软件有哪些 编辑:程序博客网 时间:2024/06/06 17:48

冒泡排序

        int[] array2 = {1,34,2,14,56,33,12,3};        for(int i = 0;i < array2.length -1;i ++){            for(int j = i + 1; j < array2.length; j ++){                if(array2[j] > array2[i]){                    int temp = array2[i];                    array2[i] = array2[j];                    array2[j] = temp;                }            }        }        for(int value : array2){            System.out.print(value + ", ");        }console:56, 34, 33, 14, 12, 3, 2, 1

插入排序

        int[] array = {1,34,2,14,56,33,12,3};        for(int i = 1;i < array.length;i ++){            int temp = array[i];            int j = i - 1;            for(;j > 0 && array[j] > temp; j --){                array[j + 1] = array[j];            }            array[j+1] = temp;        }console:1, 2, 3, 12, 14, 33, 34, 56

选择排序

        int[] array3 = {1,34,2,14,56,33,12,3};        //选择排序        for(int i = 0;i < array3.length;i ++){            int k = i;            for(int j = array3.length - 1;j > i; j --){                if(array3[j] < array3[k]){                    k = j;                }            }            int temp  = array3[i];            array3[i] = array3[k];            array3[k] = temp;        }        for(int value : array3){            System.out.print(value + ", ");        }   console:1, 2, 3, 12, 14, 33, 34, 56
0 0
原创粉丝点击