Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following pseudocode describes the algorithm for a radix sort of n decimal i

ID: 3774346 • Letter: T

Question

The following pseudocode describes the algorithm for a radix sort of n decimal integers of d digits each:
radixSort(theArray: ItemArray, n: integer, d: integer): void
for (j = d down to 1)
{
Initialize 10 groups to empty
Initialize a counter for each groups to 0
for (i = 0 through n – 1)
{
K = jth digit of theArray[i]
Place theArray[i] at the end of group k
Increase kth counter by 1
}
Replace the items in theArray with all the items in group 0,
Followed by all the items in group I, and so on.
}
}
a) Write a function called radixsort that sorts its array parameter using the above radix sort algorithm.
b) Calculate and print the number of comparisons, to verify it is in Big-O(n)
c) Write a main() function to test a worst, an averaged and a best cases in terms of time efficiency
all in c++ please....

Explanation / Answer

a) Write a function called radixsort that sorts its array parameter using the above radix sort algorithm.

Answer:-

Radix Sort :

The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is (nLogn), i.e., they cannot do better than nLogn.

Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in range from 1 to k.

What if the elements are in range from 1 to n2?

We can’t use counting sort because counting sort will take O(n2) which is worse than comparison based sorting algorithms. Can we sort such an array in linear time?
Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.

Example:
Original, unsorted list:

170, 45, 75, 90, 802, 24, 2, 66

Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

170, 90, 802, 2, 24, 45, 75, 66

Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

802, 2, 24, 45, 66, 170, 75, 90

Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 90, 170, 802

Implementation of Radix Sort Program :-
#include<iostream>
using namespace std;

// A utility function to get maximum value in arr[]
int getMax(int arr[], int n)
{
   int mx = arr[0];
   for (int i = 1; i < n; i++)
       if (arr[i] > mx)
           mx = arr[i];
   return mx;
}

// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(int arr[], int n, int exp)
{
   int output[n]; // output array
   int i, count[10] = {0};

   // Store count of occurrences in count[]
   for (i = 0; i < n; i++)
       count[ (arr[i]/exp)%10 ]++;

   // Change count[i] so that count[i] now contains actual
   // position of this digit in output[]
   for (i = 1; i < 10; i++)
       count[i] += count[i - 1];

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

   // Copy the output array to arr[], so that arr[] now
   // contains sorted numbers according to current digit
   for (i = 0; i < n; i++)
       arr[i] = output[i];
}

// The main function to that sorts arr[] of size n using
// Radix Sort
void radixsort(int arr[], int n)
{
   // Find the maximum number to know number of digits
   int m = getMax(arr, n);

   // Do counting sort for every digit. Note that instead
   // of passing digit number, exp is passed. exp is 10^i
   // where i is current digit number
   for (int exp = 1; m/exp > 0; exp *= 10)
       countSort(arr, n, exp);
}

// A utility function to print an array
void print(int arr[], int n)
{
   for (int i = 0; i < n; i++)
       cout << arr[i] << " ";
}

// Driver program to test above functions
int main()
{
   int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
   int n = sizeof(arr)/sizeof(arr[0]);
   radixsort(arr, n);
   print(arr, n);
   return 0;
}

c) Write a main() function to test a worst, an averaged and a best cases in terms of time efficiency.

Answer :-

We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case

1) Worst Case Analysis (Usually Done) :

In the worst case analysis, we calculate upper bound on running time of an algorithm. We must know the case that causes maximum number of operations to be executed. For Linear Search, the worst case happens when the element to be searched (x in the above code) is not present in the array. When x is not present, the search() functions compares it with all the elements of arr[] one by one. Therefore, the worst case time complexity of linear search would be (n).

2) Average Case Analysis (Sometimes done) :

In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by total number of inputs. We must know (or predict) distribution of cases. For the linear search problem, let us assume that all cases are uniformly distributed (including the case of x not being present in array). So we sum all the cases and divide the sum by (n+1). Following is the value of average case time complexity.

Best Case Analysis (Bogus) :

In the best case analysis, we calculate lower bound on running time of an algorithm. We must know the case that causes minimum number of operations to be executed. In the linear search problem, the best case occurs when x is present at the first location. The number of operations in the best case is constant (not dependent on n). So time complexity in the best case would be (1) .

Most of the times, we do worst case analysis to analyze algorithms. In the worst analysis, we guarantee an upper bound on the running time of an algorithm which is good information.

Program :-

#include <stdio.h>

int search(int arr[], int n, int x)
{
   int i;
   for (i=0; i<n; i++)
   {
   if (arr[i] == x)
       return i;
   }
   return -1;
}

/* Driver program to test above functions*/
int main()
{
   int arr[] = {1, 10, 30, 15};
   int x = 30;
   int n = sizeof(arr)/sizeof(arr[0]);
   printf("%d is present at index %d", x, search(arr, n, x));

   getchar();
   return 0;
}