当前位置: 首页 > news >正文

十大排序算法汇总(基于C++)

文章目录

  • 冒泡排序
  • 插入排序
  • 选择排序
  • 希尔排序
  • 堆排序
  • 快速排序
  • 归并排序
  • 桶排序
  • 计数排序
  • 基数排序
  • 算法复杂度及稳定性表格

冒泡排序

void bubbleSortUp(vector<int> &nums)
{for (int i = 0; i < nums.size() - 1; i++){bool isSwap = false;for (int j = 0; j < nums.size() - 1 - i; j++){if (nums[j] > nums[j + 1]){swap(nums[j], nums[j + 1]);isSwap = true;}}if (isSwap == false){break;}}
}void bubbleSortDown(vector<int> &nums)
{for (int i = 0; i < nums.size() - 1; i++){bool isSwap = false;for (int j = 0; j < nums.size() - 1 - i; j++){if (nums[j] < nums[j + 1]){swap(nums[j], nums[j + 1]);isSwap = true;}}if (isSwap == false){break;}}
}

插入排序

void insertSort(vector<int> &nums)
{for (int i = 1; i < nums.size(); i++){int j = i - 1;int temp = nums[i];while (j >= 0 && nums[j] > temp){nums[j + 1] = nums[j];j--;}nums[j + 1] = temp;}
}

选择排序

void selectSort(vector<int> &nums)
{for (int i = 0; i < nums.size() - 1; i++){int minIndex = i;for (int j = i + 1; j < nums.size(); j++){minIndex = nums[minIndex] > nums[j] ? j : minIndex;}if (minIndex != i){swap(nums[i], nums[minIndex]);}}
}

希尔排序

void shellSort(vector<int> &nums)
{for (int step = nums.size() / 2; step > 0; step /= 2){for (int i = step; i < nums.size(); i++){int j = i - step;int temp = nums[i];while (j >= 0 && nums[j] > temp){nums[j + step] = nums[j];j -= step;}nums[j + step] = temp;}}
}

堆排序

void maxHeapify(vector<int> &nums, int pos, int len)
{int child = 2 * pos + 1;while (child < len){if (child + 1 < len && nums[child] < nums[child + 1]){child = child + 1;}if (nums[pos] > nums[child]){return;}else{swap(nums[pos], nums[child]);pos = child;child = 2 * pos + 1;}}
}void minHeapify(vector<int> &nums, int pos, int len)
{int child = 2 * pos + 1;while (child < len){if (child + 1 < len && nums[child] > nums[child + 1]){child = child + 1;}if (nums[pos] < nums[child]){return;}else{swap(nums[pos], nums[child]);pos = child;child = 2 * pos + 1;}}
}void heapSort(vector<int> &nums)
{for (int len = nums.size(); len > 0; len--){for (int i = len / 2 - 1; i >= 0; i--){maxHeapify(nums, i, len);// minHeapify(nums, i, len);}swap(nums[0], nums[len - 1]);}
}

快速排序

void quickSort(vector<int> &nums, int begin, int end)
{if (begin >= end){return;}int left = begin;int right = end;int key = left;while (left < right){while (left < right && nums[right] >= nums[key]){right--;}while (left < right && nums[left] <= nums[key]){left++;}swap(nums[left], nums[right]);}swap(nums[key], nums[left]);key = left;quickSort(nums, begin, key - 1);quickSort(nums, key + 1, end);
}

归并排序

void merge(vector<int> &nums, int begin, int mid, int end)
{vector<int> merArray;int p1 = begin;int p2 = mid;while (p1 < mid && p2 <= end){if (nums[p1] < nums[p2]){merArray.push_back(nums[p1++]);}else{merArray.push_back(nums[p2++]);}}while (p1 < mid){merArray.push_back(nums[p1++]);}while (p2 <= end){merArray.push_back(nums[p2++]);}int index = begin;for (auto it : merArray){nums[index++] = it;}
}void mergeSortIteration(vector<int> &nums)
{for (int i = 0; i < nums.size(); i++){int start = 0;int len = 1 << i;if (len > nums.size()){break;}while (start < nums.size()){int mid = start + len - 1;if (mid > nums.size() - 1){break;}int end = min(start + 2 * len - 1, (int)nums.size() - 1);merge(nums, start, mid + 1, end);start += 2 * len;}}
}void mergeSortRecursion(vector<int> &nums, int begin, int end)
{if (begin < end){int mid = (begin + end) / 2;mergeSortRecursion(nums, begin, mid);mergeSortRecursion(nums, mid + 1, end);merge(nums, begin, mid + 1, end);}
}

桶排序

void bucketSort(vector<int> &nums)
{int maxValue = *max_element(nums.begin(), nums.end());int minValue = *min_element(nums.begin(), nums.end());int range = maxValue - minValue;int count = range / 10 + 1;vector<vector<int>> buckets(count);for (auto it : nums){int index = (it - minValue) / 10;buckets[index].push_back(it);}for (int i = 0; i < count; i++){sort(buckets[i].begin(), buckets[i].end());}int temp = 0;for (auto bucket : buckets){for (auto it : bucket){nums[temp++] = it;}}
}

计数排序

void countSort(vector<int> &nums)
{int maxValue = *max_element(nums.begin(), nums.end());int minValue = *min_element(nums.begin(), nums.end());int range = maxValue - minValue + 1;vector<int> count(range);for (auto it : nums){count[it - minValue]++;}int temp = 0;for (int i = 0; i < range; i++){while (count[i]-- > 0){nums[temp++] = i + minValue;}}
}

基数排序

void cardinalSort(vector<int> &nums)
{vector<vector<int>> buckets(19);int maxValue = *max_element(nums.begin(), nums.end());int minValue = *min_element(nums.begin(), nums.end());int temp = log10(max(abs(minValue), abs(maxValue)));for (int i = 0; i <= temp; i++){for (auto it : nums){int index = abs(it) / (int)pow(10, i) % 10;if (it < 0){index = 9 - index;}else{index = 9 + index;}buckets[index].push_back(it);}int index = 0;for (auto &bucket : buckets){for (auto it : bucket){nums[index++] = it;}bucket.clear();}}
}

算法复杂度及稳定性表格

在这里插入图片描述


http://www.mrgr.cn/news/80868.html

相关文章:

  • C语言二叉树
  • 工业一体机如何助力汽车零部件制造实现精准控制
  • 大型系统中的 MySQL 部署与优化(一)
  • arduino继电器与电机水泵的使用
  • QT:QDEBUG输出重定向和命令行参数QCommandLineParser
  • 如何制作搞笑配音视频?操作方法
  • Unity开发哪里下载安卓Android-NDK-r21d,外加Android Studio打包实验
  • Fast-Planner 改进与优化:支持ROS Noetic构建与几何A*路径规划
  • ENSP实验
  • 红队规范:减少工具上传,善用系统自带程序
  • Linux基础及命令复习
  • Makefile文件编写的学习记录(以IMX6ULL开发板的Makefile文件和Makefile.build文件来进行学习)
  • Express (nodejs) 相关
  • [LeetCode-Python版] 定长滑动窗口1(1456 / 643 / 1343 / 2090 / 2379)
  • 【NLP 16、实践 ③ 找出特定字符在字符串中的位置】
  • jmeter中的prev对象
  • Qt学习笔记第71到80讲
  • 字符串类算法
  • Linux-Profile工具
  • QT实战经验总结 连载中
  • EE308FZ_Sixth Assignment_Beta Sprint_Sprint Essay 3
  • clickhouse-副本和分片
  • 【Java】4、虚拟机 JVM
  • 华为数通最新题库 H12-821 HCIP稳定过人中
  • Cocos Creator 试玩广告开发
  • Ubuntu24版 最新安装Nvidia显卡驱动方式