桶排序c++不要用vector库函数
时间: 2023-12-21 16:32:11 浏览: 79
以下是一个不使用vector库函数的C++桶排序的示例代码:
```cpp
#include <iostream>
#include <algorithm>
void bucketSort(int arr[], int n, int maxVal) {
int bucket[maxVal + 1] = {0};
for (int i = 0; i < n; i++) {
bucket[arr[i]]++;
}
for (int i = 0, j = 0; i <= maxVal; i++) {
while (bucket[i] > 0) {
arr[j++] = i;
bucket[i]--;
}
}
}
int main() {
int arr[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int maxVal = *std::max_element(arr, arr + n);
bucketSort(arr, n, maxVal);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
阅读全文