This preview shows pages 1–2. Sign up to view the full content.
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
This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.
 Fall '08
 Staff
 Data Structures, Sort

Click to edit the document details