Motorola Interview Question
Software Engineer / DevelopersIf I understand the question correctly, I think what is asking is basically to demonstrate why a Selection Sort Algorithm has a time complexity of O(n^2) even in the worst case. The question is then making things a bit more complicated by pointing out a Binary Search Tree, but as the commenter above has said, the tree could be made into a regular linear linked list by making all left pointers equal to NULL. So, based on that assumption, a Selection Sort algorithm is O(n^2) because it must loop through the list at least twice, for each item it must find out whether is the largest element in the entire list before placing it at the end with a swap operation. This must be done using two for loops.
say the data is 1->2->3->4->5->6.
- Anonymous July 27, 2009the BST corresponding to this is also 1->2->3->4->5->6 all left pointers being NULL. Thus for a general case of n sorted data elements BST acts as a DS whose selection time is O(n). so overall complexity is O(n^2)