# notes35 - Implementation void radix(unsigned int *a, int n)...

This preview shows pages 1–2. Sign up to view the full content.

cs 251 Radix Sort -used to sort numeric keys or keys that can be represented numerically. -it assumes that each key is represented with M bits. -sorts column by column starting with the least significant column. 1. at every step, examine the bits from right to left. 2. make sure that the keys with 0s in the column appear before the keys with 1s. sort column 0 sort column 1 sort column 2 final result 10 1 = 5 0 0 0 0 00 000 00 0 = 0 0 1 0 1 01 010 01 0 = 2 1 0 1 0 10 011 11 1 = 7 1 1 1 1 11 101 01 1 = 3 0 1 1 0 11 111 Complexity: ======== -every step takes O(n) -most steps M (number of bits) -n = # of elements to sort -total time = M * O(n) = O(M*n)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Implementation void radix(unsigned int *a, int n) { //allocate scratch memory unsigned int *a2 = new unsigned int[n]; assert( a2 != NULL ); int M = 8 * sizeof( unsigned int ); for (int j = 0; j < M; j++) { int J = 0; for (int k = 0; k < n; k++) { if (a[k] & (1<<i) == 0) { a2[J] = a[k]; J++; } } //copy keys with 1s to a2 for (int k = 0; k < n; k++) { if (a[k] & (1<<i) != 0) { a2[j] = a[k]; } } //copy a2 to a memcpy(a, a2, n * sizeof(unsigned int)); } } M = O(log n)...
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 University-West Lafayette.

### Page1 / 2

notes35 - Implementation void radix(unsigned int *a, int n)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online