Sorting

AP Computer Science A· difficulty 4/5

start: pass 1: pass 2: pass 3: pass 4: [5, 2, 4, 6, 1] [2, 5, 4, 6, 1] [2, 4, 5, 6, 1] [2, 4, 5, 6, 1] [ ?, ?, ?, ?, ? ]

Insertion sort applied to <code>{5, 2, 4, 6, 1}</code> produces the partial pass states shown. What is the array AFTER pass 4 (the final pass)?

  • A

    [1, 2, 4, 6, 5]

  • B

    [2, 4, 5, 6, 1]

  • C

    [2, 1, 4, 5, 6]

  • D

    [1, 2, 4, 5, 6]

    check_circle

Explanation

Pass 4 inserts 1 at the front of the sorted prefix, shifting 2,4,5,6 right by one to give the fully sorted array [1, 2, 4, 5, 6].

Want 10 more like this — adaptive to your weak spots?

Related questions