常用排序算法Java代码

目的:总结常用的排序算法,亲测,都是OK的。虽不完善,但应付面试神马的已经足够

1. 冒泡排序

    public static void main(String[] args) {
        int[] array = new int[]{8,4,6,3,5,1,2,9,7,0};
        
        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array.length; j++) {
                if(array[i] > array[j]){
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
        
        for(int i:array)
            System.out.print(i + "\t");
    }

2. 快速排序

/**
 * 快速排序
 *
 * 基本思想:首先选定一个轴值(就是比较的基准),将待排序记录分割成独立的两部分,
 * 左侧记录的关键码都小于或等于轴值,右侧的记录关键码都大于或等于关键码,
 * 然后再对这两部分分别重复上述的过程,直到整个序列有序。
 * */

    public static void main(String[] args) {
        int[] array = new int[] { 8, 4, 6, 3, 5, 1, 2, 9, 7, 0 };
        int low = 0;
        int high = array.length - 1;
        quickSort(array, low, high);
        for (int i : array)
            System.out.print(i + "\t");
    }

    public static void quickSort(int[] array, int low, int high) {
        int l = low;
        int h = high;
        int key = array[l];
        int temp;

        while (l < h) {
            while (l < h && key <= array[h]) {
                h--;
            }

            if (l < h) {
                temp = array[l];
                array[l] = array[h];
                array[h] = temp;
            }

            while (l < h && key >= array[l]) {
                l++;
            }

            if (l < h) {
                temp = array[l];
                array[l] = array[h];
                array[h] = temp;
            }

        }

        if (l > low)
            quickSort(array, low, l - 1);
        if (h < high)
            quickSort(array, l + 1, high);
    }


3. 选择排序

/**
 * 选择排序:
 *
 * 首先以一个元素为基准,从一个方向开始扫描,以A[0]位基准,接下来从A[0]...A[n]中找出最小的元素,将其与A[0]交换。
 * 然后将其基准位置右移一位,重复上面的动作。比如,以A[1]为基准,找出A[1]...A[9]中最小的,将其与A[1]交换。
 * 一直进行到将基准位置移到数组最后一个元素时排序结束。
 *
 * */


    public static void main(String[] args) {
        int[] array = new int[] { 8, 4, 6, 3, 5, 1, 2, 9, 7, 0 };
        selectSort(array);
        for(int i:array)
            System.out.print(i + "\t");

    }

    public static void selectSort(int[] array) {
        int key;
        for (int i = 0; i < array.length; i++) {
            // 选定左侧数做基准。每排序完成一次右移一位
            key = array[i];
            
            // 将右侧所有数与基准数进行比较,选出小于基准数的数
            for (int j = i+1; j < array.length; j++) {
                if (key > array[j]) {
                    int temp = key;
                    key = array[j];
                    array[j] = temp;
                }
            }
            
            // 将最小的数与基准数交换
            array[i] = key;
        }
    }

4. 插入排序

/**
 * 插入排序
 *
 * 待排序记录 R1,R2,… ,Rn–1, Rn
 * 第一步:R1
 * 第二步:(R1 ), R2
 * 第三步:(R1 , R2), R3
 * ……
 * 第 j 步:(R1,R2,… ,Rj–1), Rj
 * ……
 * 第 n 步: (R1,R2,… ,Rn–1), Rn.
 *
 * */

    public static void main(String[] args) {
        int[] array = new int[] { 8, 4, 6, 3, 5, 1, 2, 9, 7, 0 };
        insertSort(array);
        for(int i:array)
            System.out.print(i + "\t");
    }
    
    public static void insertSort(int[] array) {
        for (int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (array[i] < array[j])
                {
                    int key = array[i];
                    array[i] = array[j];
                    array[j] = key;
                }
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值