Use el vector para implementar la clasificación de cubos
La clasificación de cubos de otros bloggers es mucho mejor, y yo también escribo uno.
Ordenar cubo.h
#pragma once
#include<iostream>
#include<vector>
using namespace std;
class BucketSort
{
public:
BucketSort();
BucketSort(int n);
void init();
void bsort();
void dispArray();
private:
vector<vector<int>> Bucket;
vector<int> array;
int n; //待排数据长度
int bucketLen;
int min;
};
BucketSort.cpp
#include "BucketSort.h"
BucketSort::BucketSort(int n) //初始化array向量,长为n,值为0
{
this->n = n;
}
void BucketSort::init() //array按0索引
{
cout << "请输入待排数组" << endl;
int num;
for (int i = 0; i < n; i++) {
cin >> num;
array.push_back(num);
}
//初始化Bucket
//首先找到输入序列的最大值和最小值 ,确定Bucket的长度 len=max/10-min/10+1
int maxnum = -0x3f3f3f, minnum = 0x3f3f3f;
for (int i = 0; i < array.size(); i++) {
if (array[i] > maxnum) {
maxnum = array[i];
}
if (array[i] < minnum) {
minnum = array[i];
}
}
this->min = minnum;
this->bucketLen = maxnum / 10 - minnum / 10 + 1;
Bucket.resize(bucketLen);
}
void BucketSort::bsort()
{
//进行桶排序
//添加各元素到对应桶
for (int i = 0; i < array.size(); i++) {
int x = Bucket[(array[i] / 10) - (min / 10)].size();
for (int j = 0; j < x; j++) {
if (array[i] < Bucket[(array[i] / 10) - (min / 10)][j]) {
Bucket[(array[i] / 10) - (min / 10)].insert(Bucket[(array[i] / 10) - (min / 10)].begin()+j,array[i]);
break;
}
}
if (Bucket[(array[i] / 10) - (min / 10)].size() > x)
continue;
Bucket[array[i] / 10 - min / 10].push_back(array[i]);
}
//将桶内元素移动到array中
int count = 0;
for (int i = 0; i < Bucket.size(); i++) {
for (int j = 0; j < Bucket[i].size();j++) {
array[count] = Bucket[i][j];
count++;
}
}
}
void BucketSort::dispArray()
{
for (int i = 0; i < array.size(); i++) {
cout << array[i] <<" ";
}
}
principal.cpp
#include"BucketSort.h"
#include<conio.h>
int main() {
cout << "输入待排数列的元素个数:";
int n = 0;
cin >> n;
cout << "依次输入待排序列:";
BucketSort bs(n);
bs.init();
cout << "待排序列为:" << endl;
bs.dispArray();
cout << endl;
bs.bsort();
cout << "排序后的序列为:" << endl;
bs.dispArray();
cout << endl;
_getch(); //让exe程序停止
return 0;
}
análisis de código
Enumere el código principal primero
void BucketSort::bsort()
{
//进行桶排序
//添加各元素到对应桶
for (int i = 0; i < array.size(); i++) {
int x = Bucket[(array[i] / 10) - (min / 10)].size();
for (int j = 0; j < x; j++) {
if (array[i] < Bucket[(array[i] / 10) - (min / 10)][j]) {
Bucket[(array[i] / 10) - (min / 10)].insert(Bucket[(array[i] / 10) - (min / 10)].begin()+j,array[i]);
break;
}
}
if (Bucket[(array[i] / 10) - (min / 10)].size() > x)
continue;
Bucket[array[i] / 10 - min / 10].push_back(array[i]);
}
//将桶内元素移动到array中
int count = 0;
for (int i = 0; i < Bucket.size(); i++) {
for (int j = 0; j < Bucket[i].size();j++) {
array[count] = Bucket[i][j];
count++;
}
}
}
El primer bucle for divide los elementos de la matriz en cubos y organiza los elementos en los cubos de pequeño a grande. La complejidad temporal de mover elementos es O ( n ) O(n)O ( n ) , suponiendo que la complejidad temporal de la clasificación esO ( nlogn ) O(nlogn)O ( n l o g n ) , un total de M cubos, n elementos, entonces hayn M {n}\over{M} enMETROnelementos, la complejidad del tiempo de clasificación es O ( M ∗ n M log ( n M ) ) O(M* \frac{n}{M} log( \frac{n}{M}))O ( M∗METROnregistro ( _ _METROn) ) La complejidad temporal total del primer ciclo for esO ( n ∗ log ( n M ) + n ) O(n*log( \frac{n}{M})+n)O ( n∗registro ( _ _METROn)+n )
El segundo bucle for es poner los elementos ordenados en el cubo de nuevo en la matriz matriz La complejidad del tiempo esO ( n ) O(n)O ( n )
Entonces, la complejidad total de la clasificación es O ( nlog ( n M ) + 2 n ) O(nlog( \frac{n}{M})+2n)O ( n l o g (METROn)+2n ) _