Given the following code:
Alg(A,n,p,r)
{
if (r ≤ p+n^1/3)
Proc1(A,p,r)
else {
t := |_(r-p+1) /3_|
Alg(A,n,p,p+t-1)
Type: Posts; User: peripatein
Given the following code:
Alg(A,n,p,r)
{
if (r ≤ p+n^1/3)
Proc1(A,p,r)
else {
t := |_(r-p+1) /3_|
Alg(A,n,p,p+t-1)
Suppose I wished to make an assertion concerning array A at the end of the i-th iteration, in other words describe A. Would it still not be correct to say that A[i+1..n] would be sorted?
How may I...
Hi,
I have the following pseudo-code for Insertion Sort:
InsertionSort(Input: integer n, array A)
{
for j = 2 to n {
newnum <- A[j]
i <- j-1
while (i > 0 and newnum <...
Hi,
I have the following pseudo-code for Bubble Sort, the correctness of which I'd like to prove via induction.
BubbleSort(input: Array A[1…n], integer n)
{
for i = n-1 to 1
{
for j =...
Unfortunately, there ARE duplicates in the array (the assignment indicates so). I wrote the following, which seems to work (I have tried it on several arrays), yet am not sure it's infallible:
...
I was actually trying to surmount the duplicate values problem by locating the first occurence of A[i]>=a thus:
BinarySearch(n,A,A[i]>=a)
That couldn't work, could it? :-)
Suppose I didn't...
I am not quite sure I am following. Let me see whether I correctly understood what you wrote. Were you implying something like this:
Range(Input: integer n, sorted array A, integer a, integer b){...
Well, in case array contains duplicate values, couldn't I simply do this?:
Range(Input: integer n, sorted array A, integer a, integer b){
max <- n
min <- 1
i <- ...
Hi,
I have an array A of n integers, sorted from min to max, and two numbers a<=b, which are known to be in A. I would like to write a pseudo-code for a procedure whose running time is c1+c2log(n)...
Hi,
In my text book the de-allocation of a dynamically allocated matrix is carried out thus:
void freeMatrix(char** mat) {
free(mat[0]);
free(mat);
}
Why is it mandatory to...
Can it be interpreted thus - if number is not divisible by 3 a=0, else a=1?
Hi,
Could anyone please explain to me the following:
int a = !(rand () % 3);
Is there a condition here? What actually forms the condition? Is it the '!'? Why does it return 0 with...
But I was no longer referring to #7. I was referring to #1 and to ways which might render it not necessarily clearer, but more sophisticated. Perhaps coming up with a better algorithm altogether. Can...
But other than that, can you come up with a more elegant/sophisticated code to yield the same output?
Preferably shorter!
I could have used more variables, agreed. But how may I improve the function I opened this thread with?
It is given that in this section the tree could not be empty.
I first coded it thus, but figured my above code suited better. What do you think?
int findMaxDepth(Node * tree){
if (!tree)...
I have. The recursion itself, the way it is processed, is giving me a hard time!
For instance, how would the program get to 12 after 9? 9->left is passed, it is NULL, then what happens? I have...
depth 0. I have tried your code and it gave 7 where it supposed to have given 6 (and whereas my code gave 6).
Hi,
I am having a hard time following the steps of the following recursion:
void inorder(Node * node) {
if (node==NULL)
return;
inorder(node->left);
printf("%d ",node->data);...
But that returns maxdepth + 1, and not the actual maxdepth.
No problem :-)
It's not an array of structs. It's a dynamically allocated array of integers.
I have an array of ints where each entry is expected to be a counter, i.e. must be initialized to zero this way or the other before the actual count begins. That would justify calloc(), wouldn't it?...
Great, thank you.
Hi,
Suppose I wished to initialize a dynamically allocated array of integers to zero. Would I do better to use calloc() or malloc + iterate over all entries setting each to zero? Which one is...