文章目录
- 排序算法复杂度总结
- 插入排序
- 希尔排序
- 归并排序
- 快速排序
- 堆排序
排序算法复杂度总结
提示:这里可以添加本文要记录的大概内容:
插入排序
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)
希尔排序
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)
归并排序
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)
快速排序
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)
堆排序
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)