7种排序算法汇总

排序算法

1. 冒泡排序

  1. 平均时间复杂度 O ( n 2 ) O(n^2) ( **最好 O ( n ) O(n) ,最差 O ( n 2 ) O(n^2) **)
  2. 空间复杂度 O ( 1 ) O(1)
  3. 稳定排序

代码如下

void BubbleSort(vector<int>& vec)
{
	if (vec.size() <= 1)
		return;

	for (int ii = 0; ii < vec.size() - 1; ++ii)
	{
		for (int jj = ii + 1; jj < vec.size(); ++jj)
		{
			if (vec[ii]>vec[jj])
			{
				swap(vec[ii], vec[jj]);
			}
		}
	}
	return;
}

动图效果
Alt text

2. 选择排序

  1. 平均时间复杂度 O ( n 2 ) O(n^2) , 最优时间复杂度 ( n 2 ) (n^2) ,最坏时间复杂度 ( n 2 ) (n^2)
  2. 空间复杂度 O ( 1 ) O(1)
  3. 不稳定排序
  4. 比较次数与序列初始化状态无关

代码

void SelectSort(vector<int>& vec)
{
	if (vec.size() <= 1)
		return;

	for (int ii = 0; ii < vec.size()-1; ++ii)
	{
		int select_val_idx = ii;
		for (int jj = ii + 1; jj < vec.size(); ++jj)
		{
			if (vec[jj] < vec[select_val_idx])
			{
				select_val_idx = jj;
			}
		}
		swap(vec[ii], vec[select_val_idx]);
	}

	return;
}

动图效果
这里写图片描述

3. 插入排序

  1. 平均时间复杂度 O ( n 2 ) O(n^2) ,最好时间复杂度 O ( n ) O(n) , 最坏时间复杂度 O ( n 2 ) O(n^2)
  2. 空间复杂度 O ( 1 ) O(1)
  3. 稳定排序
  4. 希尔排序即是多个步长的插入排序

代码

void InsertSort(vector<int>& vec)
{
	if (vec.size() <= 1)
		return;

	for (int ii = 1; ii < vec.size(); ++ii)
	{
		int insert_val = vec[ii];
		int jj = ii - 1;
		for ( ; jj >=0 ; --jj)
		{
			if (insert_val < vec[jj])
			{
				vec[jj + 1] = vec[jj];
			}
			else if (insert_val > vec[jj])
			{
				break;
			}

		}
		vec[jj + 1] = insert_val;
	}

	return;
}

动图效果
Alt text

4. 快速排序

  • 平均时间复杂度 O ( n l o g n ) O(nlogn) , 最好时间复杂度 O ( n l o g n ) O(nlogn) , 最坏时间复杂度 O ( n 2 ) O(n^2)
  • 空间复杂度 O ( l o g n ) O(logn)
  • 不稳定排序

代码

  • Partition函数用于每次确定一个元素的顺序
int Partition(vector<int>&vec, int first_idx, int last_idx)
{
	if (vec.empty() || first_idx < 0 || last_idx >= vec.size() || first_idx > last_idx)
		return -1;

	int pivot = vec[first_idx];
	int ii = first_idx;

	for (int jj = first_idx + 1; jj <= last_idx; ++jj)
	{
		if (vec[jj] < pivot)
		{
			swap(vec[++ii], vec[jj]);
		}
	}
	swap(vec[first_idx], vec[ii]);

	return ii;
}
  • 真正执行快排
void QuickSortCore(vector<int>&vec, int first_idx, int last_idx)
{
	if (first_idx < last_idx)
	{
		int mid_idx = Partition(vec, first_idx, last_idx);
		QuickSortCore(vec, first_idx, mid_idx - 1);
		QuickSortCore(vec, mid_idx + 1, last_idx);
	}

	return;
}

void QuickSort(vector<int>& vec)
{
	if (vec.size() <= 1)
		return;

	int first_idx = 0;
	int last_idx = vec.size()-1;
	QuickSortCore(vec, first_idx, last_idx);

	return;
}

动图效果
Alt text

5.归并排序

  • 平均时间复杂度 O ( n l o g n ) O(nlogn) , 最好时间复杂度 O ( n l o g n ) O(nlogn) , 最坏时间复杂度 O ( n l g n ) O(nlgn)
  • 空间复杂度 O ( n ) O(n)
  • 稳定排序

代码

void Merge(vector<int>& vec, int first_idx, int mid_idx, int last_idx)
{
	if (first_idx > mid_idx || last_idx < mid_idx)
		return;

	vector<int> tmp(last_idx - first_idx + 1, 0);

	int ii = first_idx, jj = mid_idx+1;
	int idx = 0;
	while (ii != mid_idx + 1 && jj != last_idx + 1)
	{
		if (vec[ii] > vec[jj])
			tmp[idx++] = vec[jj++];
		else
			tmp[idx++] = vec[ii++];
	}

	while (ii != mid_idx + 1)
	{
		tmp[idx++] = vec[ii++];
	}
	while (jj != last_idx + 1)
	{
		tmp[idx++] = vec[jj++];
	}

	for (int ii = 0; ii < tmp.size(); ++ii)
		vec[first_idx + ii] = tmp[ii];

	return;
}
void MergeSortCore(vector<int>&vec, int first_idx, int last_idx)
{
	if (first_idx > last_idx || first_idx < 0 || last_idx >= vec.size())
		return;

	if (first_idx < last_idx)
	{
		int mid_idx = first_idx + ((last_idx - first_idx) >> 1);
		MergeSortCore(vec, first_idx, mid_idx);
		MergeSortCore(vec, mid_idx + 1, last_idx);
		Merge(vec, first_idx, mid_idx, last_idx);
	}

	return;
}

void MergeSort(vector<int>& vec)
{
	if (vec.size() <= 1)
		return;

	int first_idx = 0;
	int last_idx = vec.size()-1;
	MergeSortCore(vec, first_idx, last_idx);

	return;
}

动图效果
Alt text

6. 堆排序

  • 时间复杂度 O ( n l o g n ) O(nlogn) , O ( n l o g n ) O(nlogn) , O ( n l o g n ) O(nlogn)
  • 空间复杂度 O ( 1 ) O(1)
  • 不稳定排序

代码

void InsertHeap(vector<int>& vec, int index)
{
	while(vec[index]>vec[(index-1)/2])
	{
		swap(vec[index], vec[(index-1)/2]);
		index = (index - 1) / 2;
	}
}

void Heapify(vector<int>& vec, int index, int heapSize)
{
	int left = 2 * index + 1;
	while(left < heapSize)
	{
		int largest = left+1<heapSize && vec[left+1]>vec[left] ? left+1 : left;
		largest = vec[largest] > vec[index] ? largest : index;
		if(largest == index)
		{
			break;
		}
		swap(vec[largest], vec[index]);
		index = largest;
		left = index*2 + 1;
	}
}
void  HeapSort(vector<int>& vec)
{
	if(vec.empty())
		return;
	
	for(int ii=0; ii<vec.size(); ++ii)
	{
		InsertHeap(vec, ii);
	}
	int heap_size = vec.size();
	swap(vec, 0, --heap_size);
	while(heap_size > 0)
	{
		heapify(vec, 0, heap_size);
		swap(vec, 0, --heap_size);
	}
}

动图效果
Alt text

7. 计数排序

Alt text

猜你喜欢

转载自blog.csdn.net/weixin_38984102/article/details/82694150
今日推荐