基于java的简单排序(下)

各种排序

public class Demo {

    public static void main(String[] args) {
        int[] a = { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };

        int[] b = { 1, 2, 3, 4, 5, 6, 7 };
        // bubbleSort(a);
        // selectSort(a);
        // insertSort(a);

        // shellSort(a, 10);
        halfSort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  ");
        }

        // System.out.println(halfQuery(b, 3));

    }

    /* 冒泡排序 */
    public static void bubbleSort(int[] a) {
        for (int i = a.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (a[j] > a[i]) {
                    swap(a, j, i);
                }
            }
        }
    }

    /* 选择排序 */
    public static void selectSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    swap(a, i, j);
                }
            }
        }
    }

    /* 插入排序 */
    public static void insertSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            for (int j = i; j > 0 && a[j] < a[j - 1]; j--) {
                swap(a, j, j - 1);
            }
        }
    }

    /* 希尔排序 */
    public static void shellSort(int[] a, int n) {
        int increment = n;
        do {
            if (increment % 2 == 0) {
                increment = increment / 2;
            } else {
                increment = increment / 2 + 1;
            }
            shellPass(a, increment);
        } while (increment > 1);
    }

    public static void shellPass(int[] a, int d) {
        for (int i = 0; i < a.length - d; i++) {
            if (a[i] > a[i + d]) {
                swap(a, i, i + d);
            }
        }
    }

    public static void swap(int[] a, int x, int y) {
        int temp = a[x];
        a[x] = a[y];
        a[y] = temp;
    }

    /* 二分法查找 */
    public static int halfQuery(int[] a, int key) {
        int low = 0, high = a.length - 1;
        int mid;
        while (low <= high) {
            mid = (low + high) / 2;
            if (a[mid] > key) {
                high = mid - 1;
            } else if (a[mid] < key) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    /* 二分法排序 */
    public static void halfSort(int[] a) {
        int low, high, mid;
        int temp;
        for (int i = 1; i < a.length; i++) {
            temp = a[i];
            low = 0;
            high = i - 1;
            while (low <= high) {
                mid = (low + high) / 2;
                if (a[mid] > a[i]) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            }

            for (int j = i - 1; j > high; j--) {
                a[j + 1] = a[j];
            }

            a[high + 1] = temp;

        }
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值