1. Modern object-oriented software makes extensive use of the malloc and free op
ID: 3550274 • Letter: 1
Question
1.
Modern object-oriented software makes extensive use of the malloc and free operations. Unfortunately, they are generally quite expensive (in time) and thus efficient routines are important.
free places freed blocks into free lists. In order to re-use memory as much as possible, malloc will generally search the free lists for a block before requesting another one from the operating system (a very expensive exercise!). A best-fit algorithm searches for the smallest free block into which the requested block will fit.
Suggest a number of ways in which free could organise the free lists and give the time complexities for
Describe any drawbacks that any method might have.
2.
Explanation / Answer
O(n^2) solution is quite trivial. You just need to iterate over the list with two iterators i,j
for(int i = 0 ; i < n ; i ++) {
for(int j = 0; j < n ; j ++) {
if(a[i] + a[i] == k) {
ans = "yes"
}
}
}
Reduction to O(nlogn) can be done by sorting the list first and then
int i = 0 ; j =n;
while(i < j)
if(a[i] + a[j] < k) {
i ++;
} else if (a[i[ + a[j] > k) {
j --;
} else {
ans = "YES"
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.