C++循环排序的实现算法(附完整源码)
循环排序是一种稳定排序算法,特别适用于小范围值的数组。循环排序的基本思想是将输入数组分成循环,每个循环都包含属于同一个元素的元素。该算法由W.D. Jones于1963年开发并发表。
下面是使用C++语言实现循环排序算法的完整源代码:
#include<iostream>
using namespace std;
void cycleSort(int arr[], int n)
{
for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) {
int item = arr[cycle_start];
int pos = cycle_start;
for (int i = cycle_start + 1; i < n; i++)
if (arr[i] < item)
pos++;
if (pos == cycle_start)
continue;
while (item == arr[pos])
pos += 1;
if (pos != cycle_start) {
swap(item, arr[pos]);
}
while (pos != cycle_start) {
pos = cycle_start;
订阅专栏 解锁全文
219



被折叠的 条评论
为什么被折叠?



