1.

What is the running time of an insertion sort algorithm if the input is pre-sorted?(a) O(N^2)(b) O(N log N)(c) O(N)(d) O(M log N)This question was addressed to me in quiz.This interesting question is from Insertion sort in portion Sorting of Data Structures & Algorithms II

Answer»

The correct choice is (c) O(N)

Easy explanation - If the INPUT is pre-sorted, the running time is O(N), because the test in the inner for loop ALWAYS fails immediately and the ALGORITHM will RUN QUICKLY.



Discussion

No Comment Found

Related InterviewSolutions