排序1

编码前分析:

  1. 采用的数据结构和算法
  2. 问题是输入规模和输出规模
  3. 机器执行速度(IO密集型问题,计算密集型问题等)

评判排序算法好坏的标准

  • 时间复杂度
    • 最好情况,最坏情况,一般情况下的时间复杂度
  • 空间复杂度
    • 是否需要另辟空间,还是原地排序
  • 算法稳定性
    • 这里的稳定性是指:如果待排序的序列中存在值相等的元素,经过排 序之后,相等元素之间原有的先后顺序不变。
冒泡排序

从头到尾,一次对相邻的两个元素进行对比,对符合要求的这两个元素进行交换

import java.util.Arrays;

public class Bubble {
    public static void sort(Comparable a[]){
        for(int i=a.length-1;i>0;i--){
            for(int j=0;j<i;j++){
                if(greater(a[j], a[i])){
                    exchange(a, i, j);
                }
            }
        }
    }

    public static boolean greater(Comparable a, Comparable b){
        return a.compareTo(b) > 0;
    }

    public static void exchange(Comparable a[], int i, int j){
       Comparable temp = a[i];
       a[i] = a[j];
       a[j] = temp;
    }

    public static void main(String[] args) {
        Integer[] a = {1, 4, 2, 6, 7, 3, 111};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
选择排序(当前元素选择跟前面的哪一个元素进行交换)

从第一个遍历到最后一个元素,每一趟找出后面的元素中最小的元素,与当前位置的元素进行交换

import java.util.Arrays;

public class Selection {
    public static void sort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            int curr = array[i];
            int minIndex = 0;
            for (int j = i; j < array.length; j++) {
                if (array[j] < curr){
                    curr = array[j];
                    minIndex = j;
                }
            }
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] a = {7, 32, 3, 1, 10};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
插入排序(当前元素选择前面的哪一个位置进行插入)

划分一个已排序区域和未排序区域,不断的从未排序区域取出一个数划分如已排序区域,并维护已排序区域的排序

import java.util.Arrays;

public class InsertSort {
    public static void sort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int curr = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > curr){
                    array[j+1] = array[j];
                }
                else
                    break;
            }
            array[j+1] = curr;
        }
    }

    public static void main(String[] args) {
        int[] a = {7, 32, 3, 1, 10};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
归并排序

利用递归分治的思想,递归是不断从中间位置划分,递归出口是当前的数组只剩下一个元素,分治是合并两个数组为有序数组

import java.util.Arrays;

public class MergeSort {
    public int[] mergeSort(int[] array) {
        if (array.length < 2) {
            return array;
        }
        int mid = array.length / 2;
        int[] lift = Arrays.copyOfRange(array, 0, mid);
        int[] right = Arrays.copyOfRange(array, mid, array.length);
        return merge(mergeSort(lift), mergeSort(right));
    }

    public int[] merge(int[] lift, int[] right) {
        int liftIndex = 0;
        int rightIndex = 0;
        int[] mergeArray = new int[lift.length + right.length];
        int prior = 0;
        while (prior < mergeArray.length) {

            if (liftIndex >= lift.length){ // 注意等号
                mergeArray[prior] = right[rightIndex++];
            } else if (rightIndex >= right.length) { // 注意等号
                mergeArray[prior] = lift[liftIndex++];
            } else if (lift[liftIndex] > right[rightIndex]) {
                mergeArray[prior] = right[rightIndex++];
            } else {
                mergeArray[prior] = lift[liftIndex++];
            }
            prior++;
        }
        return mergeArray;
    }

    public static void main(String[] args) {
        int[] array = {6, 5, 2, 6, 9, 0, 3};
        System.out.println(Arrays.toString(array));
        //main方法和要调用的方法定义在同一个类里边,可以直接调用类的静态方法
        //或者是使用 实例.方法
        MergeSort ms = new MergeSort();
        array = ms.mergeSort(array);
        System.out.println(Arrays.toString(array));
    }

}
快速排序

随机选定一个元素,将数组里面小于这个元素的值的元素放到其一边,大于这个元素的值的元素放到其另一边,不断重复,直到每个划分的数组为有序数组或者是只剩下一个元素

import java.util.Arrays;

public class QuickSort {
    public static void quickSort(int[] arr, int begin, int end) {
        // begin > end:如果要进行排序的子数组是已经有序的
        // 这个情况通过 partition 返回的 pivotIndex 肯定比 begin大
        if (arr.length <= 1 || begin > end) {
            return;
        }
        int pivotIndex = partition(arr, begin, end);
        quickSort(arr, begin, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, end);
    }

    private static int partition(int[] arr, int begin, int end) {
        int pivot = arr[end];
        int pivotIndex = begin;
        for (int i = begin; i < end; i++) {
            if (arr[i] < pivot) {
                swap(arr, i, pivotIndex);
                pivotIndex++;
            }
        }
        swap(arr, pivotIndex, end);
        return pivotIndex;
    }

    private static void swap(int[] arr, int i, int j){
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

    public static void main(String[] args) {
        int[] array = {5, 2, 6,4,3, 9, 0, 3, 4};
        System.out.println(Arrays.toString(array));
        quickSort(array, 0, array.length-1);
        System.out.println(Arrays.toString(array));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值