The following pseudocode describes the algorithm for a sort of n decimal integers of d digits each: radixNumberSort(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) Convert the above algorithm into a C++ function, using a vector for grouping by digit during the process.
solution;
#include<vector>
#include<cmath>
void radixNumberSort(int *arr, int n,int d){
int i, j, m, p=1, index, temp, count = 0;
vector<int> group[10]={};
for(i=0; i< d; i++){
m = pow(10, i+1);
p = pow(10, i);
for(j=0; j<n; j++){
temp = arr[j]%m;
index = temp/p;
group[index].push_back(arr[j]);
}
count = 0;
for(j=0; j< 10; j++){
while(!group[j].empty()){
arr[count] = *(group[j].begin());
group[j].erase(group[j].begin());
count++;
}
}
}
}
The following pseudocode describes the algorithm for a sort of n decimal integers of d digits...
Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...
Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...
Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...
17. Consider the following algorithm: procedure Algorithm(n: positive integer; di,d2.. ,dn: distinct integers) for 1 to n-1 for 1 to n-k if ddi+ then interchange di and di+ print(k, I, d,ddn-1, dn) (a) |3 points Assume that this algorithm receives as input the integer-6 and the corresponding input sequence 41 36 27 31 17 20 Fill out the table below ds (b) 1 point Assume that the algorithm receives the same input values as in part a). Once the algo-...
Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...
a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...
9. [10 points) Consider the following algorithm: procedure Algorithm(n: positive integer; ddd: distinet integers) for k:=1 to n-1 for 1-1 to n-k print(k, I, di,da...-1,dn) if ds dti then interchange dy and d (a) Assume that this algorithm receives as input the integer n 6 and the input sequence 하하하하하하, Miss ^-ruteae rehen i12|3141516 Fill out the table below: ds ds (b) Assume that the algorithm receives the same input values as in part a). Once the algorithm finishes, what...
Consider the problem where you are given an array of n digits [di] and a positive integer b, and you need to compute the value of the number in that base. In general, you need to compute For example: (1011)2 = 1(1) + 1(2) + 0(4) + 1(8) = 11; (1021)3 = 1(1) + 2(3) + 0(9) + 1(27) = 34, and (1023)4 = 3(1) + 2(4) + 0(16) + 1(64) = 75. In these examples, I give the digits...
//In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j; //the staring and ending index unsigned long freq[DIGITS]; // results }; //initialize the array void init_array(unsigned...
Create the following function in java: ALGORITHM PERMUTATION GENERATOR PermGenerator(integer n 22) li generates in lexicographical order all permutations llof the integers in the set {I.....!! Local variables: integers i //indices of permutation elements integer //for loop counter integers d., d..., Mleft to right elements of a permutation Section 4.4 Permutations and Combinations 283 1/create and write out smallest permutation for k=1 to n do end for write d.d..... //create and write out remaining permutations for k = 2 to...