Q164FreeSystemVerilog
Insertion sort in place
Interview prompt
Question
Sort a dynamic array of integers in ascending order using insertion sort.
Candidate starting point
Implementation scaffold
function automatic void insertion_sort(ref int values[]);
// TODO: implement.
endfunctionReviewed example
Trace one case
Input
values = [5, 1, 4, 2]Expected output
[1, 2, 4, 5]Each element is inserted into the already sorted prefix while equal elements retain their order.
What to cover
Requirements
- Modify the input array in place.
- Keep equal values in their original relative order.
- Handle negative values and duplicate values.
