zoukankan      html  css  js  c++  java
  • 桶排序

    #include <iostream>
    using namespace std;
    
    void BucketSort(int *A, int Max, int Size) {
    	int *B=new int [Max + 1];
    	int i, j, count = 0;
    	memset(B, 0, (Max + 1) * sizeof(int));
    	for (i = 0; i < Size; i++) {
    		j = A[i];
    		B[j] += 1;
    	}
    	for (i = 0; i <= Max; i++) {
    		if (B[i] > 0) {
    			for (j = 0; j < B[i]; j++) {
    				A[count] = i;
    				count++;
    			}
    		}
    	}
    }
    int main(int argc, const char * argv[])
    {
    	int A[] = { 1, 2, 2, 7, 4, 9, 3, 5 };
    	int Max = 9; //这里可以用一个O(n)的函数来实现,假如数组的手动输入的,则在输入的时候就可以获取到。
    	//这里直接赋值是为了排出别的因素,看着简单。
    	int Size = sizeof(A) / sizeof(int);
    	BucketSort(A, Max, Size);
    	for (int i = 0; i < Size; i++) {
    		printf("%d ", A[i]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    操作系统
    C++流类库(11)
    C++运算符重载(10)
    C++虚函数(09)
    C++向量(08)
    C++继承(07)
    ResNet实战
    ResNet,DenseNet
    经典卷积网络VGG,GoodLeNet,Inception
    CIFAR100与VGG13实战
  • 原文地址:https://www.cnblogs.com/yonglin1998/p/11780801.html
Copyright © 2011-2022 走看看