Question

Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of...

Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of 1000 elements each with the following properties:

By the way: To perform bucket sort the right way, convert the array elements to a value between 0 and 1

Array1: integers only in the range 0 through 999, already sorted in increasing order

Array2: integers only in the range 0 through 999, already sorted in decreasing order

Array3: integers only in the range 0 through 999, randomly generated

Please include your program *source code AND output* as part of your submission for this question.

1. Measure the runtime of each algorithm on each array.

2. Compare the runtime performances you've obtained in 3A and comment on them.

Please do not copy the other answers to be at risk of being downvoted. You can use C++. I mainly need a clear explanation on how each runtime compares (please don't use others' answers for this part either, or it will get downvoted).

Homework Answers

Answer #1

SOLUTION-

1) I have solve the problem in C++ code with comments and screenshot for easy understanding :)

CODE-

#include <bits/stdc++.h>
using namespace std;

//Define the maximum size of the array.
#define MAX_SIZE 1000

//Define the Counting_sort.
void Counting_Sort(int arr[], int n, int a, int b)
{
//Declare the variables.
int i, j = 0, temp_array[a] = {0};
  
//Count the occurence of the elements of the array.
for(i=0; i<n; i++)
temp_array[arr[i]-b]++;
  
//Initialize the variable i;
i=0;
  
//Insert the elements in the array.
while(i < a)
{
checkpoint:
arr[j] = b+i;
j++;
temp_array[i]--;
  
// place the a[i] until count is 0
if(temp_array[i] > 0)
goto checkpoint;
  
i++;
}
}

//Define the function find_max
//to get the maximum.
int find_max(int array[], int n)
{
//Assign the first element of the array to the variable max.
int max = array[0];
  
//Begin the for loop.
for (int i = 1; i < n; i++)
  
//Compare the array elements with the max value.
if (array[i] > max)
  
//Assign the value at the i index
//to the variable max.
max = array[i];
  
//Return the variable max.
return max;
}

//Define the helper function.
void helper(int arr[], int n, int exponent)
{

//Declare the arrays and the variables.
int ans[n], i, count[10] = {0};

for (i = 0; i < n; i++)
count[(arr[i] / exponent) % 10]++;
  
//Begin the loop and compute the cummulative count.
for (i = 1; i < 10; i++)
count[i] += count[i-1];


for (i = n - 1; i >= 0; i--)
{
ans[count[(arr[i] / exponent) % 10] - 1] = arr[i];
count[(arr[i] / exponent) % 10]--;
}

// Assigning the result to the arr pointer of main().
for (i = 0; i < n; i++)
arr[i] = ans[i];
}

//Define the Radix_sort.
void Radix_sort(int arr[], int n)
{
//Declare the variables.
int exponent, m;
  
//Call the function to get the maximum value.
m = find_max(arr, n);

//Call the function helper_function.
for (exponent = 1; m/exponent > 0; exponent *= 10)
helper(arr, n, exponent);
}

//Define the Bucket_sort().
void Bucket_sort(int a[], int n)
{
//Declare the variable.
int i, j, k, bucket[1000];
  
//Begin the for loop to initialize the buckets.
for(i = 0; i < 1000; ++i)
bucket[i] = 0;
  
//Begin the loop to fill the buckets.
for(i = 0; i < n; ++i)
++bucket[a[i]];
  
//Sort the filled bucket using the counting sort.
for(i = 0, j = 0; j < 1000; ++j)
for(k = bucket[j]; k > 0; --k)
a[i++] = j;
}

//Define the main function.
int main()
{
//Declare the clock variables.
clock_t start,end;
  
//Seed the generation of the random numbers.
srand(time(NULL));
  
//Declare the variables.
double time_spent;
int array[1000],b[1000], i, number,n;
  
//Begin the for loop and generate the random numbers.
for(i = 0; i < 1000; i++)
{
  
//Generate the random numbers.
number=rand()%1000;
array[i]=number;
b[i]=number;
  
}
  
//Display the statement.
cout<<"The time of sorting techniques for Array 1:"<<endl;
sort(array,array+1000);
  
//Begin the clock now.
start=clock();
  
//Call the counting sort.
Counting_Sort(array,1000,1000,0);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by counting sort for Array1.
cout<<"Counting sort :"<<time_spent<<" ms"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the radix_sort().
Radix_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by Radix sort for Array1.
cout<<"Radix sort :"<<time_spent<<" ms"<<endl;
  
start=clock();
  
//Call the bucket_sort().
Bucket_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;

//Display the time taken by Bucket sort for Array1.
cout<<"Bucket sort :"<<time_spent<<" ms"<<endl;
  
  
/***********Array 2***********************/
  
for(i = 0; i < 1000; i++)
{
number=rand()%1000;
array[i]=number;
b[i]=number;
  
}
  
//Display the statement.
cout<<"The time of sorting techniques for Array 2:"<<endl;
sort(array,array+1000);
reverse(array,array+n);
  
//Begin the clock now.
start=clock();
  
//Call the counting sort.
Counting_Sort(array,1000,1000,0);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by counting sort for Array2.
cout<<"Counting sort :"<<time_spent<<" ms"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the radix_sort().
Radix_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by Radix sort for Array2.
cout<<"Radix sort :"<<time_spent<<" ms"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the bucket_sort().
Bucket_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by Bucket sort for Array2.
cout<<"Bucket sort :"<<time_spent<<" ms"<<endl;
  
  
/*********************Array 3************/
  
for(i = 0; i < 1000; i++)
{
  
number=rand()%1000;
array[i]=number;
b[i]=number;
  
}
  
//Display the statement.
cout<<"The time of sorting techniques for Array 3:"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the counting sort.
Counting_Sort(array,1000,1000,0);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by counting sort for Array3.
cout<<"Counting sort :"<<time_spent<<" ms"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the radix_sort().
Radix_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by Radix sort for Array3.
cout<<"Radix sort :"<<time_spent<<" ms"<<endl;
  
//Begin the clock now.
start=clock();
  
//Call the bucket sort.
Bucket_sort(array,1000);
  
//Now end the clock.
end=clock();
  
//Computing the difference of the start and the end time.
time_spent=(double)(end-start)/CLOCKS_PER_SEC;
  
//Convert the difference in milliseconds.
time_spent=time_spent*1000.0;
  
//Display the time taken by Bucket sort for Array3.
cout<<"Bucket sort :"<<time_spent<<" ms"<<endl;
  
//Return the value for the main function.
return 0;

}

SCREENSHOT-

2)

The comparison of the sorting techniques as per the sample output obtained is given as follows:

As per the first array, the time complexity of the sorting techniques is in the following order:

Bucketsort (0.018 ms) < Countingsort (0.019 ms) < Radixsort (0.138 ms)

As per the second array, the time complexity of the sorting techniques is in the following order:

Bucketsort (0.019 ms) < Countingsort (0.021 ms) < Radixsort (0.134 ms)

As per the third array, the time complexity of the sorting techniques is in the following order:

Bucketsort (0.02 ms) < Countingsort (0.021 ms) < Radixsort (0.132 ms)

Hence, on average the counting and the bucket sort are doing better as compared to the radix sort.

Since, after every execution, the output will be different, but the performance of the bucket and the counting sort is always better than the radix sort.

Thus, among the three sorting techniques, the better sorting techniques to use are bucket and counting.


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...