GATE CSE
First time here? Checkout the FAQ!
x
0 votes
113 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 (13.2k points)   | 113 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 Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1484 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1408 Points

  5. Niraj Singh 2

    1311 Points

  6. Rupendra Choudhary

    1194 Points

  7. rahul sharma 5

    1148 Points

  8. Debashish Deka

    1112 Points

  9. srestha

    932 Points

  10. Arjun

    930 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1306 Points

  3. junaid ahmad

    502 Points

  4. sudsho

    410 Points

  5. akankshadewangan24

    392 Points


23,361 questions
30,068 answers
67,376 comments
28,385 users