首页 > 代码库 > 桶排序

桶排序

 1 #include <iostream> 2 using namespace std; 3  4  5 // 6 //   This file contains the C++ code from Program 3.5 of 7 //   "Data Structures and Algorithms 8 //    with Object-Oriented Design Patterns in C++" 9 //   by Bruno R. Preiss.10 //11 //   Copyright (c) 1998 by Bruno R. Preiss, P.Eng.  All rights reserved.12 //13 //   http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/programs/pgm03_05.cpp14 //15 unsigned int const m = 10;16 17 void BucketSort(unsigned int a[], unsigned int n)18 {19     int buckets[m];20 21     for (unsigned int j = 0; j < m; ++j)22         buckets[j] = 0;23     for (unsigned int i = 0; i < n; ++i)24         ++buckets[a[i]];25     for (unsigned int i = 0, j = 0; j < m; ++j)26         //for (unsigned int k = buckets[j]; k > 0; --k)27         if(buckets[j]!=0)28             a[i++] = j;29 }30 int main() {31 32     unsigned int a[4] = {6,3,4,2};33     BucketSort(a,4);34 35     for (int i = 0;i < 4;i++) {36         cout << a[i] << " ";37     }38     cout << endl;39 40     system("pause");41     return 0;42 }

 

桶排序