排序总结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)
相关推荐
MChine慕青15 分钟前
顺序表与单链表:核心原理与实战应用
linux·c语言·开发语言·数据结构·c++·算法·链表
qq_1955516919 分钟前
代码随想录70期day7
java·开发语言
Sam-August1 小时前
【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者?
java·spring cloud·dubbo
塔中妖1 小时前
【华为OD】查找接口成功率最优时间段
算法·链表·华为od
塔中妖1 小时前
【华为OD】最大子矩阵和
算法·华为od·矩阵
麦兜*1 小时前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
努力学习的小廉1 小时前
深入了解linux系统—— 线程同步
linux·服务器·数据库·算法
数据爬坡ing1 小时前
从挑西瓜到树回归:用生活智慧理解机器学习算法
数据结构·深度学习·算法·决策树·机器学习
ytadpole2 小时前
揭秘设计模式:命令模式-告别混乱,打造优雅可扩展的代码
java·设计模式
luoganttcc2 小时前
小鹏汽车 vla 算法最新进展和模型结构细节
人工智能·算法·汽车