CO322: DS & A (Simple Yet) Efficient Algorithms: Dhammika Elkaduwe
CO322: DS & A (Simple Yet) Efficient Algorithms: Dhammika Elkaduwe
Dhammika Elkaduwe
University of Peradeniya
return i != records.length;
}
return i != records.length;
}
Another example:
static int power_of(int a, int k) {
/* function should return a^k
* Do I have preconditions?
*/
int i = 1;
for(; k < 1; k--)
i = i * a;
return i;
}
Can we improve:
I n2k = (nk )2 and n2k+1 = n2k ∗ n
I Can you use this observation to make the above algorithm
better? (code using recursion/iteration)
I What is the time complexity of the new version
Exercises:
I Implement the merge sort, using Java
I What is the time complexity of merge sort?
I What is the space complexity of merge sort? needs additional
space)
Exercises:
I Implement the merge sort, using Java
I What is the time complexity of merge sort?
I What is the space complexity of merge sort? needs additional
space)
return merged;
}
Assume that the first element of the array is selected as the pivot.
Ideally we need a pivot that would halve the problem.
I What is the best case? What is the corresponding time
complexity?
I What is the worst case? What is the corresponding time
complexity?
I What is the average case? What is the corresponding time
complexity?