+1 vote
in Data Structures & Algorithms II by (110k points)
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

1 Answer

+1 vote
by (614k points)
selected by
Best 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.

Related questions

Welcome to TalkJarvis QnA, a question-answer community website for the people by the people. On TalkJarvis QnA you can ask your doubts, curiosity, questions and whatever going in your mind either related to studies or others. Experts and people from different fields will answer.