Recent questions tagged cormen

0 0 votes
0 0 answers
430
430 views
Argue the correctness of HEAP-INCREASE-KEY using the following loop invariant:At the start of each iteration of the while loop of lines $4–6$, the subarray $A[1..A.heapsi...
0 0 votes
0 0 answers
408
408 views
Why do we bother setting the key of the inserted node to $-\infty$ in line $2$ of MAX-HEAP-INSERT when the next thing we do is increase its key to the desired value?
1 1 vote
0 0 answers
530
530 views
Write pseudo code for the procedures HEAP-MINIMUM, HEAP-EXTRACT-MIN, HEAP-DECREASE-KEY, and MIN-HEAP-INSERT that implement a min-priority queue with a min-heap.
0 0 votes
0 0 answers
417
417 views
HEAP-INCREASE-KEY(A,i,key) 1 if key < A[i] 2 error “new key is smaller than current key” 3 A[i] = key 4 while i 1 and A[parent(i)] < A[i] 5 exchange A[i] with A[parent(i...
0 0 votes
0 0 answers
771
771 views
HEAP-EXTRACT-MAX(A) 1 if A.heap-size < 1 2 error “heap underflow” 3 max=A 4 A =A[A.heapsize] 5 A.heapsize=A.heapsize-1 6 MAX-HEAPIFY(A,1) 7 return max Illustrate th...
0 0 votes
0 0 answers
505
505 views
Show that when all elements are distinct, the best-case running time of HEAPSORT is $\Omega(n\lg\ n)$.
0 0 votes
0 0 answers
471
471 views
Show that the worst-case running time of HEAPSORT is $\Omega(n\lg\ n)$.
0 0 votes
0 0 answers
435
435 views
What is the running time of HEAPSORT on an array $A$ of length $n$ that is already sorted in increasing order? What about decreasing order?
0 0 votes
0 0 answers
540
540 views
Argue the correctness of HEAPSORT using the following loop invariant:At the start of each iteration of the for loop of lines $2–5$,the subarray $A[1..i]$ is a max-heap co...
0 0 votes
0 0 answers
525
525 views
HEAPSORT(A) 1 BUILD-MAX-HEAP(A) 2 for i = A.length down to 2 3 exchange A with A[i] 4 A.heapsize=A.heapsize – 1 5 MAX-HEAPIFY(A,1)illustrate the operation of HEAPSORT on...
0 0 votes
0 0 answers
391
391 views
Show that there are at most $\lceil n/2^{h+1}\rceil$ nodes of height $h$ in any $n-$element heap.
0 0 votes
0 0 answers
377
377 views
Why do we want the loop index $i$ in line $2$ of BUILD-MAX-HEAP to decrease from $\lfloor A.length/2 \rfloor$ to 1 rather than increase from 1 to $\lfloor A.length/2 \rfl...
0 0 votes
0 0 answers
698
698 views
BUILD-MAX-HEAP(A) 1 A.heapsize=A.length 2 for i=A.length/2 downto 1 3 MAX-HEAPIFY(A,i)Using Figure $6.3$ as a model, illustrate the operation of BUILD-MAX-HEAP on the arr...
0 0 votes
0 0 answers
436
436 views
Show that the worst-case running time of MAX-HEAPIFY on a heap of size $n$ is $\Omega(lg\ n)$.(Hint: For a heap with $n$ nodes, give node values that cause MAXHEAPIFY to ...
0 0 votes
0 0 answers
419
419 views
The code for MAX-HEAPIFY is quite efficient in terms of constant factors, except possibly for the recursive call in line 10, which might cause some compilers to produce i...
0 0 votes
0 0 answers
295
295 views
What is the effect of calling MAX-HEAPIFY$(A,i)$ for $i A.heapsize/2$.
0 0 votes
0 0 answers
297
297 views
What is the effect of calling MAX-HEAPIFY$(A, i)$ when the element $A[i]$ is larger than its children?
0 0 votes
0 0 answers
479
479 views
Starting with the procedure MAX-HEAPIFY, write pseudocode for the procedure MIN-HEAPIFY$(A, i )$, which performs the corresponding manipulation on a minheap. How does the...
0 0 votes
0 0 answers
574
574 views
MAX-HEAPIFY(A,i) 1 l=Left(i) 2 r=Right(i) 3 if l <= A.heapsize and A[l] A[i] 4 largest=l 5 else largest = i 6 if r <= A.heapsize and A[r] A[largest] 7 largest=r...
0 0 votes
2 2 answers
675
675 views
Prove that $n!=\omega(2^n)$ and $n!=o(n^n)$.
0 0 votes
0 0 answers
396
396 views
Give an algorithm that determines the number of inversions in any permutation on $n$ elements in $\Theta (n\ lg\ n)$ worst-case time. (Hint: Modify merge sort.)
0 0 votes
0 0 answers
281
281 views
What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer.
0 0 votes
1 1 answer
600
600 views
What array with elements from the set $\{1,2,\dots n\}$ has the most inversions? How many does it have?
0 0 votes
1 1 answer
455
455 views
List the five inversions of the array $\langle 2,3,8,6,1\rangle$
0 0 votes
1 1 answer
893
893 views
Use mathematical induction to show that when $n$ is an exact power of $2$, the solution of the recurrence$T(n) = \begin{cases} 2 \text{, if n=2, } ...
0 0 votes
1 1 answer
584
584 views
Describe a $\Theta(n\ lg\ n)$ time algorithm that, given a set $S$ of $n$ integers and another integer $x$, determines whether or not there exist two elements in $S$ whos...
0 0 votes
1 1 answer
1.2k
1.2k views
Observe that the while loop of the INSERTION-SORT procedure uses a linear search to scan (backward) through the sorted subarray $A[i\dots j-1]$ Can we use a binary search...
0 0 votes
0 0 answers
503
503 views
Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence $A$ is sorted, we can check the midpoint of the sequence against $v$ and elimin...
0 0 votes
1 1 answer
708
708 views
We can express the insertion sort as a recursive procedure as follows.In order to sort $A[1\dots n]$, we recursively sort $A[1 \dots n-1]$ and then insert $A[n]$ into the...
0 0 votes
0 0 answers
786
786 views
Rewrite the MERGE procedure so that it does not use sentinels, instead of stopping once either array $L$ or $R$ has had all its elements copied back to $A$ and then copyi...