排序总结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)
相关推荐
_.Switch11 分钟前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
带带老表学爬虫28 分钟前
java数据类型转换和注释
java·开发语言
千里码aicood35 分钟前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
彭于晏68943 分钟前
Android广播
android·java·开发语言
程序员-珍1 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
自由的dream1 小时前
0-1背包问题
算法
2401_857297911 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
福大大架构师每日一题2 小时前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes
金灰2 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
菜鸟一皓2 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea