算法的一些小栗子1(插入排序)

来源:互联网 发布:双十一网络瘫痪 编辑:程序博客网 时间:2024/05/17 22:37

直接插入排序

//直接插入排序public class InsertSort {    public void sort(int[] a) {        System.out.println("排序之前:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }        System.out.println();        //开始直接插入排序        int temp;        for (int i = 1; i < a.length; i++) {            temp = a[i];//新遍历的值,等待插入到前面的有序数组            int j;            for (j = i - 1; j >= 0; j--) {                //将大于temp的数往后移一位                if (a[j] > temp) {                    a[j + 1] = a[j];                } else {                    break;                }            }            a[j + 1] = temp;        }        System.out.println("排序之后:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    public static void main(String[] args) {        InsertSort insertSort = new InsertSort();        int[] a = {22, 18, 36, 47, 55, 88, 12, 8, 39, 66, 75, 0};        insertSort.sort(a);    }}

二分法插入排序

//二分法插入排序public class BinaryInsertSort {    public void sort(int[] a) {        System.out.println("排序之前:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }        System.out.println();        int temp;        for (int i = 0; i < a.length; i++) {            temp = a[i];            int left = 0;            int right = i - 1;            int mid = 0;            while (left <= right) {                mid = (left + right) / 2;                if (temp < a[mid]) {                    right = mid - 1;                } else {                    left = mid + 1;                }            }            for (int j = i - 1; j >= left; j--) {                //比left右边大的值往后移一位,等待temp插入                a[j + 1] = a[j];            }            if (left != i) {                a[left] = temp;            }        }        System.out.println("排序之后:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    public static void main(String[] args) {        BinaryInsertSort binaryInsertSort = new BinaryInsertSort();        int[] a = {23, 66, 47, 70, 68, 90, 77, 88, 64, 53, 14, 3, 0, 99};        binaryInsertSort.sort(a);    }}


希尔排序

//希尔排序public class HeerSort {    public void sort(int[] a) {        System.out.println("排序之前:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }        System.out.println();        int d = a.length / 2;//默认增量        int temp;        while (true) {            for (int i = 0; i < d; i++) {                for (int j = i; j + d < a.length; j += d) {                    if (a[j] > a[j + d]) {                        temp = a[j];                        a[j] = a[j + d];                        a[j + d] = temp;                    }                }            }            if (d == 1) {                break;            }            d--;        }        System.out.println("排序之后:");        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    public static void main(String[] args) {        int[] a = {10, 14, 22, 34, 25, -9, 64, 72, 81, 1};        HeerSort heerSort = new HeerSort();        heerSort.sort(a);    }}


原创粉丝点击