
![int CountOccur (int i,j) { int mid, count; if j <i return 0 else mid Floor (j)/2) if S[mid] x Count 1 CountOccur(i,mid-1) Cou](http://img.homeworklib.com/images/16da7cb1-6e58-4b98-a3eb-7948046d2255.png?x-oss-process=image/resize,w_560)
1.
int countOccur(int S[], int x, int n){
int count = 0;
for(int i = 0;i<n;i++){
if(S[i] == x)
count++;
}
return count;
}
In the above code, S is the array which contains all the digits, x is the one with which we want to match, n is the number of items inside the array S.
2. [2, 3, 6 , 2, 7, 8, 2, 9] x = 2
So here n = 8
So we need to check for each value of ranging from 0 to 7
i = 0, match, count = 1
i = 1, not match
i = 2, not match
i = 3, match, count = 2
i = 4, not match
i = 5 not match
i = 6 match, count = 3
i = 7 not match
return count as 3, 3 is the answer from the illustration.
3. For the following problem, time complexity would be O(n), where n is the number of elements in the array.
Friend: If you have
any doubts regarding the solution to the problem, do let me know in
the comment section.
Thanks
Suppose you have an array S indexed from 1 to n which contains n numbers, not...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...
Algorithm ComparisonCountingSorting(A[0..n-1], S[0..n-1] ) //Input: Array A[0..n-1] of orderable values //Output: Array S[0..n-1] of A's values in ascending order for i ← 0 to n – 1 do Count[i] ← 0 for i ← 0 to n – 2 do for j ← i + 1 to n – 1 do if A[i] < A[j] Count[j] ← Count[j] + 1 // increment counter of larger else Count[i] ← Count[i] + 1 for i ← 0 to n –...
Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
1)
can any one please givem the code for this
a) If n is a power of 2, as it is in Figure 9-3, you would merge
pairs of individual entries, starting at the
beginning of the array. Then you would return to the beginning of
the array and merge pairs of twoentry
subarrays. Finally, you would merge one pair of four-entry
subarrays. Notice that the subarrays
in each pair of subarrays contain the same number of entries.
In general,...
Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...
C++, THIS IS FOR AN UNSORTED ARRAY.
I'm trying to implement an "if" statement that will check if
there are 2 or more modes in a data set. I tried checking "if
(count == total)" then it would increment "pos" by 1 and put the
other mode in the list (m[pos-1] = values[index]), but it kept
giving me the same number multiple times. For instance, if the data
set was {1,5,10,4,7,3,8,3,9,10} (the 3 and 10 occur twice) then i
would...
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...