In: Computer Science
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation.
#include <iostream>
using namespace std;
void radixLSD_help(int *items, int length, int bit)
{
// – Count number of items for each bucket.
// – Figure out where each bucket should be stored (positions
// of the first and last element of the bucket in the scratch
// array).
// – Copy each item to the corresponding bucket (in the scratch
// array).
// – Copy the scratch array back into items.
}
void radixLSD(int *items, int length)
{
int bit_per_item = sizeof(int) * 8;
int bit;
for (bit = 0; bit < bit_per_item; bit++)
{
radixLSD_help(items, length, bit);
cout << "Done with bit " << bit;
}
}
void sort(int *A,int n)
{
radixLSD(A, n);
}
void sort( int *A, int n);
int main()
{
int i, offset, j;
int B[10000000];
time_t t;
srand( (unsigned) time( &t ));
offset = rand()%10000000;
for( i = 0; i< 10000000; i++ )
{
B[i] = ((91*i)%10000000) + offset;
}
printf("Prepared array of 10 million integers; calling sort\n");
sort( B[], 10000000 );
printf("finished sort, now check result\n");
for( i=0, j=0; i < 10000000; i++ )
if( B[i] != i+ offset ) j++;
if( j == 0 )
printf("Passed Test\n");
else
printf("Failed Test. %d numbers wrong.\n", j );
}
Updated and working code:
#include <iostream>
using namespace std;
void radixLSD_help(int *items, int length, int bit)
{
// – Count number of items for each bucket.
// – Figure out where each bucket should be stored (positions
// of the first and last element of the bucket in the scratch
// array).
// – Copy each item to the corresponding bucket (in the scratch
// array).
// – Copy the scratch array back into items.
}
void radixLSD(int *items, int length)
{
int bit_per_item = sizeof(int) * 8;
int bit;
for (bit = 0; bit < bit_per_item; bit++)
{
radixLSD_help(items, length, bit);
cout << "Done with bit \n" << bit;
}
}
void sort(int *A,int n)
{
radixLSD(A, n);
}
void sort( int *A, int n);
int main()
{
int i, offset, j;
int B[10000000];
time_t t;
srand( (unsigned) time( &t ));
offset = rand()%10000000;
for( i = 0; i< 10000000; i++ )
{
B[i] = ((91*i)%10000000) + offset;
}
printf("Prepared array of 10 million integers; calling sort\n");
sort( B, 10000000 );
printf("finished sort, now check result\n");
for( i=0, j=0; i < 10000000; i++ )
if( B[i] != i+ offset ) j++;
if( j == 0 )
printf("Passed Test\n");
else
printf("Failed Test. %d numbers wrong.\n", j );
}