- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
I want to describe an algorithm that given an unsorted array $B$ that stores $m$ integers, and any integer number $y$, determines if there are two elements of the array of which the quotient is equal to $y$. The time complexity of the algorithm should be $O(m \log m)$.
We have to use efficient techniques for sorting and finding an element in a sorted array.
For the sorting could we use the following algorithm? (Thinking)
How could we check if there are two elements of the array of which the quotient is equal to $y$? (Thinking)
I want to describe an algorithm that given an unsorted array $B$ that stores $m$ integers, and any integer number $y$, determines if there are two elements of the array of which the quotient is equal to $y$. The time complexity of the algorithm should be $O(m \log m)$.
We have to use efficient techniques for sorting and finding an element in a sorted array.
For the sorting could we use the following algorithm? (Thinking)
Code:
procedure Heapify(A[i..j]) {
/* At the beginning, Α[i..j-1] is partially sorted, and after the execution of Heapify() A[i..j] is partially sorted*/
m = j;
while ((leftchild(m) ≥ i AND A[leftchild(m)] < A[m]) OR (rightchild(m) ≥ i AND A[rightchild(m)] < A[m]) {
if (rightchild(m) ≥ i ) {
if (A[leftchild(m)] < A[rightchild(m)])
p = leftchild(m)
else p = rightchild(m);
}
else p = i;
swap(Α[m], A[p]);
m = p;
}
}