This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: if: n is very large m is very small compared to n Implementation void bucketSort( int *a, int n ) { //search for the range int min = a[0]; int max = a[0]; for (int i = 0; i < n; i++) { if (min > a[i]) { min = a[i]; } if (max < a[i]) { max = a[i]; } } int m = max  min + 1; //range //create buckets int *buckets = new int[ m ]; //initialize buckets with 0s memset( buckets, m * sizeof(int), 0 ); //create histogram for (int i = 0; i < n; i++) { buckets[ a[i]  min ]++; } //put histogram back into array for (int i = 0, int j = 0; j < m; j++) { for (int k = 0; k < buckets[j]; k++) { a[i] = j + min; i++ } } } //end of bucket sort...
View
Full Document
 Fall '08
 Staff
 Data Structures, Sort, Max, bucket sort, int max, int min, buckets

Click to edit the document details