8种排序算法的Java实现

8种排序算法

数据结构中常用的8种排序算法:冒泡、选择、插入、希尔、快速、归并、基数、堆排序。

算法性能对比

表格参考自尚硅谷视频

算法最好情况最坏情况平均空间复杂度排序方式稳定性
冒泡O(n)O( n 2 n^2 n2)O( n 2 n^2 n2)O(1)In-place稳定
选择O( n 2 n^2 n2)O( n 2 n^2 n2)O( n 2 n^2 n2)O(1)In-place不稳定
插入O(n)O( n 2 n^2 n2)O( n 2 n^2 n2)O(1)In-place稳定
希尔O(n l o g 2 log^2 log2n)O(n l o g 2 log^2 log2n)O(nlogn)O(1)In-place不稳定
归并O(nlogn)O(nlogn)O(nlogn)O(n)Out-place稳定
快速O(nlogn)O( n 2 n^2 n2)O(nlogn)O(logn)In-place不稳定
基数O(n*k)O(n*k)O(n*k)O(n+k)Out-place稳定
O(nlogn)O(nlogn)O(nlogn)O(1)In-place不稳定
算法代码
  1. 冒泡排序
public class BubbleSort {

    private static int count = 0;

    // 第一次将最小的数放在了最左边
    public void bubbleSort(int[] nums){
        int len = nums.length;
        boolean flag = false;
        for(int i = 0;i< len-1;i++){
            for(int j = i+1;j<len;j++){
                if(nums[i] > nums[j]){
                    flag = true;
                    count++;
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
            if(!flag){
                break;
            }else{
                flag = false;
            }
            for (int num : nums) {
                System.out.print(num+"\t");
            }
            System.out.println();
        }
        System.out.printf("执行了%d次",count);
    }
    // 第一次将最大的数放在最右边
    public void bubbleSort2(int[] nums){
        int len = nums.length;
        boolean flag = false;
        for(int i = 0;i< len-1;i++){
            for(int j = 0;j<len-1-i;j++){
                count++;
                if(nums[j] > nums[j+1]){
                    flag = true;
                    int temp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = temp;
                }
            }
            if(!flag){
                break;
            }else{
                flag = false;
            }
            for (int num : nums) {
                System.out.print(num+"\t");
            }
            System.out.println();
        }
        System.out.printf("执行了%d次",count);
    }

    public static void main(String[] args) {
        new BubbleSort().bubbleSort2(new int[]{10,2,3,1,7,5,4,6,8});
    }
}
  1. 选择排序
public class SelectSort {
    public static void main(String[] args) {
        int[] nums = {3,4,2,9,6,1,-2,8,5,7};

        for(int i = 0;i < nums.length;i++){
            int min = nums[i];
            int index = i;
            for(int j = i+1; j < nums.length;j++){
                if(nums[j] < min){
                    min = nums[j];
                    index = j;
                }
            }
            if(index != i){
                nums[index] = nums[i];
                nums[i] = min;
            }
        }
        for (int num : nums) {
            System.out.printf(num+"\t");
        }
    }
}
  1. 插入排序
public class InsertSort {
	/*
	* 需要移动数组
	* 速度比冒泡快些
	* */
    public static void insertSort(int[] num){

        for(int i=1;i<num.length;i++){
            int index  = i-1;
            int val = num[i];
            // 先和它前面的一个数比较
            while (index >=0 && val < num[index]){
                num[index+1] = num[index];
                index--;
            }
            // 相等时说明没有动
            if(index + 1 != i)
                num[index+1] = val;
        }
        for (int i : num) {
            System.out.print(i+"\t");
        }
    }

    public static void main(String[] args) {
        int[] nums = {3,4,2,9,6,1,-2,8,5,7};
        insertSort(nums);
    }
}
  1. 希尔排序
public class ShellSort {

    // 交换式 效率低·
    public static void shellSort(int[] nums){
        int length = nums.length;
        for(int gap = length / 2;gap > 0;gap /= 2){
            // 对每一组进行排序
            for(int i = gap;i<length;i++){
                for (int j = i-gap; j >= 0; j -= gap) {
                    if(nums[j] > nums[j+gap]){
                        int temp = nums[j+gap];
                        nums[j+gap] = nums[j];
                        nums[j] = temp;
                    }
                }
            }
        }
        for (int num : nums) {
            System.out.print(num+"\t");
        }
    }

    // 移动式 速度快
    public static void shellSortByMove(int[] nums){
        int length = nums.length;
        for(int gap = length / 2;gap > 0;gap /= 2){
            // 对每一组进行插入排序
            for(int i = gap;i<length;i++){
                int j = i-gap;
                int val = nums[i];
                while(j>=0 && nums[j] > val){
                    nums[j+gap] = nums[j];
                    j -= gap;
                }
                nums[j+gap] = val;
            }
        }
        for (int num : nums) {
            System.out.print(num+"\t");
        }
    }

    public static void main(String[] args) {
        int[] nums = {3,4,2,9,6,1,-2,8,5,7};
        shellSortByMove(nums);
    }
}
  1. 归并排序
public class MergeSort {

    public static void main(String[] args) {
        int[] nums = {2,8,1,4,3,7,9};
        mergeSort(nums,0,nums.length-1,new int[nums.length]);
        System.out.println(Arrays.toString(nums));
    }

    public static void mergeSort(int[] nums,int left,int right,int[] temp){
        if(left < right){
            // 分
            int mid = left+(right-left)/2;
            mergeSort(nums,left,mid,temp);
            mergeSort(nums,mid+1,right,temp);
            // 治
            merge(nums,left,mid,right,temp);
        }
    }

    /**
     *
     * @param arr 原数组
     * @param left 左索引
     * @param mid 中间元素索引
     * @param right 右索引
     * @param temp 临时数组
     */
    public static void merge(int[] arr,int left,int mid,int right,int[] temp){
        int i = left,j = mid+1;
        int t = 0;
        //1. 比较两边有序数组,依次将较小的插入到temp
        while(i <= mid && j<= right){
            if(arr[i] <= arr[j]){
                temp[t++] = arr[i++];
            }else{
                temp[t++] = arr[j++];
            }
        }
        //2. 将两边剩余元素插入到temp
        while(i <= mid){
            temp[t++] = arr[i++];
        }
        while(j <= right){
            temp[t++] = arr[j++];
        }
        //3. 将temp拷贝到 arr
        t = 0;
        for (int k = left; k <= right; k++) {
            arr[k] = temp[t++];
        }
    }
}
  1. 基数排序
public class RadixSort {
    public static void main(String[] args) {
        int[] nums = {20,8,15,104,322,79,98};
        radixSort(nums);
    }

    public static void radixSort(int[] nums){
        // 首先要找到最大的数的长度
        int mmaxNum = nums[0];
        for(int i=1;i<nums.length;i++){
            if(mmaxNum < nums[i])
                mmaxNum = nums[i];
        }
        int length = mmaxNum+ "".length();
        // 定义10个桶,二维数组, 数字0-9
        int[][] bucket = new int[10][nums.length];
        // 定义存放每个桶中的元素的数组
        int[] dataCount = new int[10];

        for (int i = 0; i < length; i++) {
            for(int j=0;j<nums.length;j++){
                int num = nums[j] / (int)Math.pow(10,i) % 10;
                // 将每一个数按照对应为放入到对应的桶中取去
                bucket[num][dataCount[num]] = nums[j];  // 存的是nums[j],不是num
                dataCount[num]++;
            }
            // 将分好的数重新放到原数组中
            int index = 0;
            // 遍历每一个痛,不为空时
            for(int k = 0;k< bucket.length;k++){
                if(dataCount[k] > 0){
                    for(int l = 0;l < dataCount[k];l++){
                        nums[index++] = bucket[k][l];
                    }
                }
                dataCount[k] = 0;
            }
        }
        System.out.println(Arrays.toString(nums));
    }
}
  1. 快速排序
public class QuickSort {
    public static void main(String[] args) {
        int[] nums = {4,6,1,3,5,2,7};
        quickSort(nums,0, nums.length-1);
        for (int num : nums) {
            System.out.print(num+"\t");
        }
    }
	// 第一种
    public static void quickSort(int[] nums,int left,int right){
        // 终止条件
        if (left >= right)return;
        // 以第一个数为基准数
        int num = nums[left];
        int i = left,j = right;

        while(i < j){
            // 先从右至左寻找第一个小于基准的数,
            while(i<j && nums[j] >= num)j--;
            if(i<j)
                nums[i++] = nums[j]; //并将基准位置替换成该数, 并将左指针后移一位
            // 再从左向右寻找第一个比基准大的元素
            while(i<j && nums[i] < num)i++;
            if(i<j)
                nums[j--] = nums[i];// 将该数放置右边对应位置,并将右指针左移一位
        }
        // 一次排序过后,最终i==j,并将基准放置该待的地方
        nums[i] = num;
        quickSort(nums,left,i-1);
        quickSort(nums,i+1,right);

    }
    
	// 第二种实现方式
    public static int[] quick_sort(int[] num, int l, int r){
        //r为数组元素总个数,last下标等于r-1
        int first=l,last=r-1,key=num[first];
        while(first<last){
            while(first<last&&num[last]>=key){
                --last;
            }
            //如果值小于 key分界值 交换
            num[first]=num[last];
            while(first<last&&num[first]<key){
                ++first;
            }
            //如果值大于key分界值 交换
            num[last]=num[first];
        }
        num[first]=key;
        //递归左右部分进行快排
        if (first>l) {
            num=quick_sort(num, l, first);
        }
        if (first+1<r){
            num=quick_sort(num,first+1,r);
        }
        return num;
    }

}
  1. 堆排序
public class HeapSort {

    public static void adjust(int[] arr,int i,int len){
        int temp = arr[i]; //记录当前值
        for(int k = i * 2+1;k < len;k = k*2+1){
            // 比较左右子树哪个大,记录较大值的索引
            if(k+1 <len && arr[k] < arr[k+1]){
                k++;
            }
            // 将较大值放到父节点
            if(arr[k] > temp){
                arr[i] = arr[k];
                i = k;
            }else {
                break;
            }
        }
        // 更新父节点的值
        arr[i] = temp;
    }

    public static void heapSort(int[] arr){
        // 将数组构建一个堆
        for(int i = arr.length/2-1;i >= 0 ;i--){
            adjust(arr,i, arr.length);
        }

        // 排序,先交换
        for(int j = arr.length-1;j >= 0 ;j--){
            int temp = arr[j];
            arr[j] = arr[0];
            arr[0] = temp;
            adjust(arr,0,j);
        }
        System.out.println(Arrays.toString(arr));
    }

    public static void main(String[] args) {
        int[] arr = {1,7,4,6,8,5,9,3};
        heapSort(arr);
    }
}

如有书写错误的地方,欢迎指正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中,可以通过直接插入排序、希尔排序和堆排序来实现排序算法。 直接插入排序的Java实现可以参考以下代码: ```java import java.util.Arrays; public class InsertSortDemo { public static void insertSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j > key) { arr[j + 1 = arr[j]; j--; } arr[j + 1 = key; } } public static void main(String[] args) { int[] arrTest = {0, 1, 5, 8, 3, 7, 4, 6, 2}; System.out.println("before: " + Arrays.toString(arrTest)); insertSort(arrTest); System.out.println("after: " + Arrays.toString(arrTest)); } } ``` 希尔排序的Java实现可以参考以下代码: ```java import java.util.Arrays; public class ShellSortDemo { public static void shellSort(int[] arr) { int n = arr.length; for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int key = arr[i]; int j = i; while (j >= gap && arr[j - gap > key) { arr[j = arr[j - gap]; j -= gap; } arr[j = key; } } } public static void main(String[] args) { int[] arrTest = {0, 1, 5, 8, 3, 7, 4, 6, 2}; System.out.println("before: " + Arrays.toString(arrTest)); shellSort(arrTest); System.out.println("after: " + Arrays.toString(arrTest)); } } ``` 堆排序的Java实现可以参考以下代码: ```java import java.util.Arrays; public class HeapSortDemo { public static void heapSort(int[] arr) { int n = arr.length; // 构建大顶堆 for (int i = n / 2 - 1; i >= 0; i--) { heapify(arr, n, i); } // 依次将堆顶元素与末尾元素交换,并重新构建堆 for (int i = n - 1; i > 0; i--) { int temp = arr = arr[i]; arr[i = temp; heapify(arr, i, 0); } } public static void heapify(int[] arr, int n, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[left > arr[largest]) { largest = left; } if (right < n && arr[right > arr[largest]) { largest = right; } if (largest != i) { int temp = arr[i]; arr[i = arr[largest]; arr[largest = temp; heapify(arr, n, largest); } } public static void main(String[] args) { int[] arrTest = {0, 1, 5, 8, 3, 7, 4, 6, 2}; System.out.println("before: " + Arrays.toString(arrTest)); heapSort(arrTest); System.out.println("after: " + Arrays.toString(arrTest)); } } ``` 以上是三种常见排序算法Java实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java实现七种经典排序算法](https://blog.csdn.net/qq_27498287/article/details/126049089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值