Question: USE RUBY PROGRAMMING LANGUAGE FOR THE SOLUTION Problem 6 Write a function insert(x, a) that takes an integer x and an array a of integers
USE RUBY PROGRAMMING LANGUAGE FOR THE SOLUTION
Problem 6
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]
Problem 7
Write the function insertion_sort(a) that takes an array of numbers and returns an array of the same values in nondecreasing order, without modifying a. Your function should use the insert function written in the previous problem. >> b = Array.new(12) {Random.rand(100)} => [30, 81, 43, 95, 24, 38, 64, 56, 74, 70, 33, 60] >> insertion_sort(b) => [24, 30, 33, 38, 43, 56, 60, 64, 70, 74, 81, 95] >> b => [30, 81, 43, 95, 24, 38, 64, 56, 74, 70, 33, 60] >> insertion_sort(b) == b.sort => true
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
