GATE CSE
First time here? Checkout the FAQ!
x
0 votes
104 views

Consider the array of size n. the first (n – 1) elements are already sorted. What is the worst case time complexity to insert a nth element in an array after insertion the array should be in sorted order

  1.   O(1)
  2.   O(n)
  3.   O(n log n)
  4.   O(n2)
asked in Algorithms by Veteran (12.9k points)   | 104 views
i think this is same as last iteration of insertion sort

to insert nth element into already sorted array of size (n-1),we have to compare maximum with (n-1) elements and shift (n-1) in worst case..so T(n) should be O(n)

am i correct??

1 Answer

+2 votes
Best answer
it's take O(n)

because in worst case , we need to shift (n-1) elements
answered by Active (1.3k points)  
selected by


Top Users Apr 2017
  1. akash.dinkar12

    3752 Points

  2. Divya Bharti

    2618 Points

  3. Deepthi_ts

    2162 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Sanjay Sharma

    1646 Points

  7. Debashish Deka

    1614 Points

  8. Shubham Sharma 2

    1610 Points

  9. Prashant.

    1554 Points

  10. Kapil

    1528 Points

Monthly Topper: Rs. 500 gift card

22,100 questions
28,082 answers
63,368 comments
24,203 users