GATE CSE
First time here? Checkout the FAQ!
x
0 votes
105 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 (13k points)   | 105 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 May 2017
  1. akash.dinkar12

    3140 Points

  2. pawan kumarln

    1606 Points

  3. sh!va

    1580 Points

  4. Arjun

    1316 Points

  5. Devshree Dubey

    1230 Points

  6. Angkit

    1020 Points

  7. Debashish Deka

    1012 Points

  8. Bikram

    970 Points

  9. LeenSharma

    796 Points

  10. srestha

    658 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. pawan kumarln

    232 Points

  2. jjayantamahata

    106 Points

  3. joshi_nitish

    106 Points

  4. Ahwan

    96 Points

  5. Aditya GN

    63 Points


22,717 questions
29,045 answers
65,029 comments
27,454 users