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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!