Page 231 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 231
Fundamentals of Data Structures
Notes 5:
6:
7:
8:
9:092
After step 2, the order is: 100, 500, 007, 008, 012, 092.
Step 3, using the MSB:
0:007, 008, 012, 092
1: 100
2:
3:
4:
5: 500
6:
7:
8:
9:
The final list is the sorted one: 007, 008, 012, 092, 100, 500.
Now let use the implementation of radix sort in c:
#include < iostream >
using namespace std;
#define MAX 100
void print(int *a, int n) //Prints the numbers of an array
{
int i;
for (i = 0; i < n; i++)
cout << “ ” << a[i];
}
void radixsort(int *a, int n)
{
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i<n; i++)
{
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
224 LOVELY PROFESSIONAL UNIVERSITY