桶排序的vector实现

使用vector来实现桶排序

其他博主的桶排序优秀很多,我自己也写一个。

BucketSort.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] <<" ";
	}
}

main.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;
}

代码分析

先列出主要代码

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++;
		}
	}
}

第一个for循环将array中的元素分桶并将桶中的元素按从小到大排列,移动元素的时间复杂度为 O ( n ) O(n) O(n),假设排序的时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn),共有M个桶,n个元素,则平均每个桶里有 n M {n}\over{M} Mn个元素,则排序时间复杂度为 O ( M ∗ n M l o g ( n M ) ) O(M* \frac{n}{M} log( \frac{n}{M})) O(MMnlog(Mn))第一个for循环总的时间复杂度为 O ( n ∗ l o g ( n M ) + n ) O(n*log( \frac{n}{M})+n) O(nlog(Mn)+n)
第二个for循环是将桶中排好序的元素放回到array数组中时间复杂度为 O ( n ) O(n) O(n)

所以排序的总复杂度为 O ( n l o g ( n M ) + 2 n ) O(nlog( \frac{n}{M})+2n) O(nlog(Mn)+2n)

猜你喜欢

转载自blog.csdn.net/qq_32577169/article/details/105874017