桶排序降序
Bucket sort descending order?
我正在尝试使用c++对数组按升序和降序排序的程序。我只使用桶排序创建升序:
void bucketSort (int data[], int n)
{
int x = 65537; //range is [1,65536]
int buckets[x]; //Create empty buckets
for (int i = 0; i < x; i++) //Initialize all buckets to 0
buckets[i] = 0;
//Increment the # of times each element is present in the input
//array. Insert them in the buckets
for (int i = 0; i < n; i++)
buckets[data[i]]++;
//Sort using insertion sort and link
for (int i = 0, j = 0; j < x; j++)
for (int k = buckets[j]; k > 0; k--)
data[i++] = j;
}
但是我不知道如何按降序排列。
你在这里做的是计数排序,而不是桶排序。
现在,要按降序对元素进行排序,更改第三个for
循环如下:
for(int i = 0, j = x; j >= 0; j--))
std::reverse(data, data + n)
结尾,
或按降序迭代桶。(j = x - 1 to 0
)
相关文章:
- 我正在尝试按降序对地图进行排序,但没有得到预期的结果?
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 如何按矩阵行的总和降序对矩阵进行排序 C++.
- 插入按降序排序
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 我怎么不能按课程代码的降序对数字进行排序
- 如何使用布尔函数按降序对数组进行排序?
- 如何按降序排序?
- 数组将排序排序为降序而不是升序
- 如何按降序对标准数组进行排序 - C++ 11.
- 按降序C 排序矢量
- 使用QuickSort算法以降序排序向量
- 偶数升序排序,奇数降序排序
- 按类向量的降序排序
- 按降序排序的最佳方式
- 快速排序在对降序-升序数据进行排序时的奇怪行为
- C++使用单独的函数按降序对数组进行排序
- 按降序对双链接列表进行排序
- 在c++中按降序插入排序
- 桶排序降序