Homework Help Question & Answers

؟ 5- How could you design a Three-Pass-Radix sort algorithm to sort the following group of...

5- How could you design a Three-Pass-Radix sort algorithm to sort the following group of integ containing three digits: 666,6

؟

5- How could you design a Three-Pass-Radix sort algorithm to sort the following group of integ containing three digits: 666,6,66,323, 13, 333, 111, 23, 100, 102
0 0
Report
Answer #1

-> 666, 6,66,323,13,333, 111,23,100, 102 o to 9. Step 1: Define 10 queues U In 7 8 6 9 4 2 step 2: Based on least significantstep 4 Based hundreds place insert in queue. 100 ,102,006 102,006,!!!,013 !!!,013,323,023, 333, 666,066 66 23 13 6 102 3 33

///defining a function to which i pass the array list and size of n

void radixsort(int list[], int n) {
//get a max number for which the loop has to run i.e for 2 digit number we need to run 2 times i.e upto tens place   

int m = getMax(list, n);

int exp;

//for every digit place i am calling function count sort
for (exp = 1; m / exp > 0; exp *= 10)
countSort(list, n, exp);
}

//getting the maxvalue from array

int getMax(int list[], int n) {
int mx = list[0];
int i;
for (i = 1; i < n; i++)
if (list[i] > mx)
mx = list[i];
return mx;
}

//function to insert the elements in queue based on digit --i.e units digit and tens place

void countSort(int list[], int n, int exp) {
//defining the output queue and of size n

int output[n];
int i, count[10] = { 0 };

//loop to count the number of elements in the particular queue place ..i.e at queue 1 how many elements are //inserted

for (i = 0; i < n; i++)
count[(list[i] / exp) % 10]++;

for (i = 1; i < 10; i++)
count[i] += count[i - 1];

///inserting in the queue based on digits place in the queue output   

for (i = n - 1; i >= 0; i--) {
output[count[(list[i] / exp) % 10] - 1] = list[i];
count[(list[i] / exp) % 10]--;
}

//coping the elements  and modifing the queue--list

for (i = 0; i < n; i++)
list[i] = output[i];
}

c code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>


int getMax(int list[], int n) {
int mx = list[0];
int i;
for (i = 1; i < n; i++)
if (list[i] > mx)
mx = list[i];
return mx;
}

void countSort(int list[], int n, int exp) {
int output[n];
int i, count[10] = { 0 };

for (i = 0; i < n; i++)
count[(list[i] / exp) % 10]++;

for (i = 1; i < 10; i++)
count[i] += count[i - 1];

for (i = n - 1; i >= 0; i--) {
output[count[(list[i] / exp) % 10] - 1] = list[i];
count[(list[i] / exp) % 10]--;
}

for (i = 0; i < n; i++)
list[i] = output[i];
}

void radixsort(int list[], int n) {
int m = getMax(list, n);

int exp;
for (exp = 1; m / exp > 0; exp *= 10)
countSort(list, n, exp);
}

void print(int list[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", list[i]);
}

int main()
{
   //defining the array list
int list[] = {666,6, 66, 323, 13, 333, 111, 23, 100,102 };
int i, n = sizeof(list) / sizeof(list[0]);

printf("List of numbers before sort: \n");
for(i = 0; i<10; i++)
printf("%d\t", list[i] );
//calling the function
radixsort(list, n);

printf("\n\nList of numbers after sort: \n");
print(list, n);
printf("\n\n");
return 0;
}

List of numbers before sort: 666 6 66 323 13 333 111 23 100 182 List of numbers after sort: 13 23 66 100 102 111 323 333 666

Know the answer?
Add Answer to:
؟ 5- How could you design a Three-Pass-Radix sort algorithm to sort the following group of...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
Free Homework App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.