行业知识
【数据结构】计数排序 & 排序系列所有源代码 & 复杂度分析(终章)
2026-11-25 18:22:43
来源:互联网转载
���录
一,计数排序
1,基本思想
2,思路实现
3,计数排序的特性总结:
二,排序算法复杂度及稳定性分析
三,排序系列所有源代码
Sort.h
Sort.c
Stack.h
Stack.c
一,计数排序
计数排序也叫非比较排序;
1,基本思想
计数排序又称为鸽巢原理,是对哈希直接定址法的变形应用
操作步骤:
1,统计相同元素出现次数
2,根据统计的结果将序列回收到原来的序列中
图解原理:
对这样一个不需要比较的排序就完成了;
2,思路实现
// 计数排序void CountSort(int* arr, int n){int i = 0;int max = arr[0], min = arr[0];//找最大,最小值for (i = 0; i max){max = arr[i];}if (arr[i] 然后我们运行测试一下:
可以看到是有序的,选择排序就 OK 了;
3,计数排序的特性总结:
1, 计数排序在数据范围集中时,效率很高,但是适用范围及场景有限
2.,时间复杂度:O(N+K)
3, 空间复杂度:O(N)
4, 稳定性:稳定
二,排序算法复杂度及稳定性分析
| 排序方法 | 平均情况 | 最好情况 | 最坏情况 | 辅助空间 | 稳定性 |
| 冒泡排序 | O(N^2) | O(N) | O(N^2) | O(1) | 稳定 |
| 选择排序 | O(N^2) | O(N^2) | O(N^2) | O(1) | 不稳定 |
| 直接插入排序 | O(N^2) | O(N) | O(N^2) | O(1) | 稳定 |
| 希尔排序 | O(NlongN)~O(N^2) | O(N^1.3) | O(N^2) | O(1) | 不稳定 |
| 堆排序 | O(NlongN) | O(NlongN) | O(NlongN) | O(1) | 不稳定 |
| 归并排序 | O(NlongN) | O(NlongN) | O(NlongN) | O(N) | 稳定 |
| 快速排序 | O(NlongN) | O(NlongN) | O(N^2) | O(N) | 不稳定 |
| 计数排序 | O(N+K) | O(N+K) | O(N+K) | O(K) | 稳定 |
三,排序系列所有源代码
Sort.h
#pragma once#include#include#include#include"Stack.h"//打印void PrintSort(int* arr, int n);//插入排序void InsertSort(int* arr, int n);//希尔排序void HillSort(int* arr, int n);//选择排序void SeleSort(int* arr, int n);//堆排序void HeapSort(int* arr, int n);//向下调整void DownAdjust(int* arr, int n, int i);冒泡排序//void BubblSort(int* arr, int n);//快速排序void QuickSort(int* arr, int begin, int end);//三数取中int middle(int* arr, int left, int right);//快慢指针法int PartSort3(int* arr, int left, int right);//挖坑法int PartSort2(int* arr, int left, int right);//霍尔排序int PartSort1(int* arr, int left, int right);//快速排序(非递归)void QuickNon(int* arr, int begin, int end);//归并排序void MergerSort(int* arr, int begin, int end);//归并排序(非递归)void MergerSortNon(int* arr, int begin, int end);// 计数排序void CountSort(int* arr, int n);
Sort.c
#define _CRT_SECURE_NO_WARNINGS 1#include"Sort.h"//打印void PrintSort(int* arr, int n){int i = 0;for (i = 0; i = 0){if (arr[end] >= tmp){//交换Swap(&arr[end], &arr[end+1]);end--;}else{break;}}arr[end + 1] = tmp;}}//希尔排序void HillSort(int* arr, int n){int gap = n;int i = 0;while (gap > 1){gap = gap / 2;for (i = 0; i = 0){if (arr[end] >= tmp){//交换Swap(&arr[end], &arr[end + gap]);end -= gap;}else{break;}}arr[end + gap] = tmp;}}}//选择排序void SeleSort(int* arr, int n){int begin = 0, end = n - 1;while (begin 1){//交换Swap(&arr[0], &arr[n - 1]);n--;//向下调整DownAdjust(arr, n, 0);}}//三数取中int middle(int* arr, int left, int right){//int mid = (left +right)/ 2;//随机数取中int mid = left + (rand() % (right - left));if (arr[left] arr[right]){return right;}else{return left;}}}//霍尔排序int PartSort1(int* arr, int left, int right){//三数取中int ret = middle(arr, left, right);Swap(&arr[left], &arr[ret]);int keyi = left;while (left = end){return NULL;}if (end - begin = end){return;}//插入STPush(&ps, end);STPush(&ps, begin);//栈不为空就进去while (!STEmpty(&ps)){int left = STInsert(&ps);//栈顶元素STPop(&ps);//删除int right = STInsert(&ps);STPop(&ps);int keyi = PartSort1(arr, left, right);//排序[left,keyi-1] & [keyi+1,right]if (keyi + 1 a);ps->a = NULL;ps->top = ps->capacity = 0;}//插入void STPush(ST* ps, STDataType x){assert(ps);if (ps->top == ps->capacity){ps->capacity = ps->top == 0 ? 4 : ps->capacity*2;ps->a = (STDataType*)realloc(ps->a,sizeof(STDataType)*ps->capacity);}ps->a[ps->top] = x;ps->top++;}//删除void STPop(ST* ps){assert(ps);assert(ps->top > 0);ps->top--;}//返回栈顶STDataType STInsert(ST* ps){assert(ps);assert(ps->top > 0);return ps->a[ps->top-1];}//数量int STSize(ST* ps){assert(ps);return ps->top;}//判断是否为空int STEmpty(ST* ps){assert(ps);if (ps->top == 0){return 1;}else{return 0;}} 同志们!排序的知识就到这里了!
后面博主会陆续更新;
如有不足之处欢迎来补充交流!
完结。。
上一篇:金蝶服务器怎么设置方法?