经典排序算法-Java版

网上容易找到C/C++实现的排序算法,Java的虽然有,不过大都过于繁琐。这段时间因为准备面试,在网上查找了一些排序算法的Java版,整理在此。

1.冒泡排序

时间复杂度O(n^2),稳定度:稳定。
public static void bubbleSort(int[] input) {
    for(int i = 0; i < input.length; ++i) {
        for(int j = 0; j < input.length - i - 1; ++j) {
            if(input[j] > input[j + 1]) {
                //交换
                int temp = input[j];
                input[j] = input[j + i];
                input[j + 1] = temp;
            }
        }
    }
}

2.选择排序

时间复杂度O(n^2),稳定度:不稳定。
public static void selectionSort(int[] input) {
    for(int i = 0; i < input.length; ++i) {
        int minIndex = i;
        for(int j = i; j < input.length; ++j) {
            if(input[j] < input[minIndex]) {
                minIndex = j;
            }
        }
        if(minIndex != i) {
            //交换
            int temp = input[i];
            input[i] = input[minIndex];
            input[minIndex] = temp;
        }
    }
}

3.插入排序

时间复杂度O(n^2),稳定度:稳定。
public static void insertionSort(int[] input) {
    for(int i = 1; i < input.length; ++i) {
        for(int j = i; j > 0; --j) {
            if(input[j] < input[j - 1]) {
                //交换
                int temp = input[j];
                input[j] = input[j - 1];
                input[j - 1] = temp;
            }
        }
    }
}
写完代码后发现,插入排序跟冒泡排序好像,只不过冒泡是依此向上(向后)进行交换,而插入是向下(向前)。

4.快速排序

时间复杂度O(nlogn),稳定度:不稳定。
public static void quickSort(int[] input, int low, int high) {
    if(low < high) {//递归终止条件
        int pivot = partition(input, low, high);
        quickSort(input, low, pivot - 1);
        quickSort(input, pivot + 1, high);
    }
}
public static int partition(int[] input, int low, int high) {
    int key = input[low];
    while(low < high) {
        while(low < hign && input[high] > key) {//此处,必需先从high开始向下遍历。
            --high;
        }
        input[low] = input[high];
        while(low < high && input[low] < key) {
            ++low;
        }
        input[high] = input[low];
    }
    input[low] = key;
    return low;
}

5.归并排序

时间复杂度O(nlogn),稳定度:稳定。
public static void mergeSort(int[] input, int low, int high) {
    if(low < high) {
        int mid = (high - low) / 2;
        mergeSort(input, low, mid);
        mergeSort(input, mid + 1, high);
        merge(input, low, mid, high);
    }
}
public static void merge(int[] input, int low, int mid, int high) {
    int i = low, j = mid + 1, k = 0;
    int[] temp = new int[high - low + 1];
    while(i <= mid && j <= high) {
        if(input[i] < input[j]) {
            temp[k++] = input[i++];
        }else{
            temp[k++] = input[j++];
        }
    }
    while(i <= mid) {
        temp[k++] = input[i++];
    }
    while(j <= high) {
        temp[k++] = input[j++];
    }
    for(i = 0; i < temp.length; ++i) {
        input[low + i] = temp[i];
    }
}

6.堆排序

时间复杂度O(nlogn),稳定度:不稳定。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值