Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1.
(Hint: represent a value x as (i, j, k) where x = i · (n^2) + j · n + k.)
#include<iostream>
using namespace std;
int main()
{
int n,p,i,j;
cout<<"Enter the value of n : ";
cin>>n;
cout<<"Enter the number of values you want to
enter : ";
cin>>p;
int arr[n*n*n]={0};
cout<<"Enter the value one by one : ";
for(i=0;i<p;i++)
{
cin>>j;
arr[j]=1;
}
cout<<"Sorted values are : ";
for(i=0;i<n*n*n;i++)
{
if(arr[i]==1)
cout<<i<<" ";
}
}
Sample Output:-
Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1. (Hint: represent...
Java Match the sorting algorithm with its time complexity, where n is the number of elements in the collection and k is the range of values. (This is a one-for-one match where an answer can only match one description.) Group of answer choices selection sort [ Choose ] O(n + k) O(n) to O(n^2) O(n log n) O(n^2) bubble sort [ Choose...
When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...
(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...
2. (10) Given a ”black-box” worst-case linear-time median subroutine, give a simple, linear-time algorithm that solves the selection problem for any given k, where 1 ≤ k ≤ n. Once again, an algorithm should be presented first, followed by its analysis.
3) How much time does the following “algorithm” require as a function of n? l=0 for i=1 to n do for j=1 to i do for k=j to n do l = l + 1
In Java, Bucket sort is a sorting algorithm that has O(n) performance on average. I want you to write a function to perform bucket sort on a list of strings. I also want you to test your function to see if it really work in linear time on average. This code will need to be tested. Finally, I want you to compare the performance of your function with Javasort method for lists.
1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...
Let S1, S2,...,Sk be k different sequences whose elements have integer keys in the range [0, N − 1], for some parameter N ≥ 2. Describe an algorithm running in O(n + N) time for sorting all the sequences (not as a union), where n denotes the total size of all the sequences.
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow 0 to n - 2 do for j leftarrow i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.
Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for i = 1 to 5n^2 do p = p times i return p (b) Algorithm for-loop(n): s = 0 for i = 1 to n do for j = I to n do s = s + i return s (c) Algorithm WhileLoop(n): x = 0; j = 2; while (j = n){x = x+ 1; j =j times 2;}