排序总结Java

文章目录


排序算法复杂度总结

提示:这里可以添加本文要记录的大概内容:

插入排序

java 复制代码
public class Sort {
    // 插入排序
    public int[] inserSort(int[] nums)
    {
        for (int i = 1; i < nums.length; i ++)
        {
            int pre = i - 1;
            int cur = nums[i];
            while (pre >= 0 && nums[pre] > cur)
            {
                nums[pre + 1] = nums[pre];
                pre -= 1;
            }
            nums[pre + 1] = cur;
        }
        return nums;
    }
  }

算法分析:

  • 稳定性:稳定
  • 时间复杂度 :最佳: O ( n ) O(n) O(n);最差: O ( n 2 ) O(n^2) O(n2);平均: O ( n 2 ) O(n^2) O(n2)
  • 空间复杂度 : O ( 1 ) O(1) O(1)

希尔排序

java 复制代码
public class Sort {
    // 希尔排序
    public int[] shellSort(int[] nums)
    {
        int n = nums.length;
        int gap = n / 2;
        while (gap > 0)
        {
            for (int i = gap; i < n; i ++)
            {
                int pre = i - gap;
                int cur = nums[i];
                while (pre >= 0 && nums[pre] > cur)
                {
                    nums[pre + gap] = nums[pre];
                    pre -= gap;
                }
                nums[pre + gap] = cur;
            }
            gap /= 2;
        }
        return nums;
    }
  }

算法分析:

  • 稳定性:不稳定
  • 时间复杂度 :最佳: O ( n l o g n ) O(nlogn) O(nlogn);最差: O ( n 2 ) O(n^2) O(n2);平均: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度 : O ( 1 ) O(1) O(1)

归并排序

java 复制代码
public class Sort {
    // 归并排序
    public int[] merge(int[] nums1, int[] nums2)
    {
        int[] num = new int[nums1.length + nums2.length];
        int id = 0, id1 = 0, id2 = 0;
        while (id1 < nums1.length && id2 < nums2.length)
        {
            if (nums1[id1] < nums2[id2])
            {
                num[id] = nums1[id1];
                id1 ++;
            }else{
                num[id] = nums2[id2];
                id2 ++;
            }
            id ++;
        }
        if (id1 < nums1.length)
        {
            num[id ++] = nums1[id1 ++];
        }
        if (id2 < nums2.length)
        {
            num[id ++] = nums2[id2 ++];
        }
        return num;
    }
    public int[] mergeSort(int[] nums)
    {
        int n = nums.length;
        if (n == 1) return nums;
        int mid = n / 2;
        int[] nums1 = Arrays.copyOfRange(nums,0,mid);
        int[] nums2 = Arrays.copyOfRange(nums,0,n);
        return merge(mergeSort(nums1),mergeSort(nums2));
    }
  }

算法分析:

  • 稳定性:稳定
  • 时间复杂度 :最佳: O ( n l o g n ) O(nlogn) O(nlogn);最差: O ( n l o g ) O(nlog) O(nlog);平均: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度 : O ( n ) O(n) O(n)

快速排序

java 复制代码
public class Sort {
    // 快速排序
    public int partion(int[] nums, int low, int high)
    {
        int index = high;
        int sindex = low;
        for (int i = low; i < high; i ++)
        {
            if (nums[i] <= nums[index])
            {
                int tmp = nums[sindex];
                nums[sindex] = nums[i];
                nums[i] = tmp;
                sindex ++;
            }
        }
        int tmp = nums[index];
        nums[index] = nums[sindex];
        nums[sindex] = tmp;
        return sindex;
    }
    public void quick(int[] nums, int low, int high)
    {
        while (low < high)
        {
            int index = partion(nums, low, high);
            quick(nums,0,index - 1);
            quick(nums,index + 1,high);
        }
    }
    public int[] quickSort(int[] nums)
    {
        quick(nums,0, nums.length - 1);
        return nums;
    }
  }

算法分析:

  • 稳定性:不稳定
  • 时间复杂度 :最佳: O ( n l o g n ) O(nlogn) O(nlogn);最差: O ( n l o g ) O(nlog) O(nlog);平均: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度 : O ( l o g n ) O(logn) O(logn)

堆排序

java 复制代码
public class Sort {
	// 堆排序
	public int heapLen;
    public void swap(int[] nums, int i, int j)
    {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
    public void heapify(int[] nums, int largest)
    {
        int left = 2*largest + 1;
        int right = 2*largest + 2;
        int index = largest;
        if (left < heapLen && nums[left] > nums[largest])
        {
            index = left;
        }
        if (right < heapLen && nums[right] > nums[largest])
        {
            index = right;
        }
        if (index != largest)
        {
            swap(nums,index,largest);
            heapify(nums,largest);
        }
    }
    public void maxheapify(int[] nums)
    {
        for (int i = nums.length / 2 - 1; i >= 0; i --)
        {
            heapify(nums,i);
        }
    }
    public int[] heapSort(int[] nums)
    {
        heapLen = nums.length;
        maxheapify(nums);
        for (int i = heapLen - 1; i >= 0; i --)
        {
            swap(nums,0,i);
            heapLen --;
            heapify(nums,i);
        }
        return nums;

    }
  }

算法分析:

  • 稳定性:不稳定
  • 时间复杂度 :最佳: O ( n l o g n ) O(nlogn) O(nlogn);最差: O ( n l o g ) O(nlog) O(nlog);平均: O ( n l o g n ) O(nlogn) O(nlogn)
  • 空间复杂度 : O ( l o g n ) O(logn) O(logn)
相关推荐
憧憬成为java架构高手的小白1 小时前
LangGraph4j 实战(下)
java
天诰1 小时前
SA-K-11 检索会消失吗与四个未解问题
java·服务器·人工智能·python·机器学习·ai编程
这个昵称不容易5 小时前
SpringBoot「衣依」服装销售平台:144个Java文件180个接口,从挑衣服到收货评价整条链路都在
java·springboot·毕设
计算机毕设定制辅导-无忧学长5 小时前
《基于微信小程序的居家医疗陪护预约系统设计与实现》
java·spring boot·微信小程序·uniapp·居家医疗陪护预约系统
文丑颜不良啊6 小时前
LeetCode2221~2240题目解析
java·leetcode
秋饼7 小时前
Computer Use Agent Java 落地实战:感知-行动循环、执行器与安全围栏生产级实现
java·ai·技术分享·后端开发
Navigator_Z7 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH7 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶8 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划