Question: Use Ruby programming for the following: Write a function insert(x, a) that takes an integer x and an array a of integers in nondecreasing order
Use Ruby programming for the following:
Write a function insert(x, a) that takes an integer x and an array a of integers in nondecreasing order and returns a new sorted array that includes x and the integers of a. Your function should not modify a.
>> a = [2, 4, 5, 9, 12]
=> [2, 4, 5, 9, 12]
>> insert(5, a)
=> [2, 4, 5, 5, 9, 12]
>> insert(10, a)
=> [2, 4, 5, 9, 10, 12]
>> insert(20, a)
=> [2, 4, 5, 9, 12, 20]
>> a
=> [2, 4, 5, 9, 12]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
