Question: Please solve this question in MATLAB using only basic functions (no functions from toolboxes). After the picture of the problem and instructions are some hints
Please solve this question in MATLAB using only basic functions (no functions from toolboxes). After the picture of the problem and instructions are some hints and guidelines for the following parts.

Problem 2: Merge Sort
Part 1: Nested function (Function within a function) Lec 8
% combine2Arrays: Think about placing array entries in the right positions.
% Arrays input are both SORTED. (Makes things easier)
% Input arrays can be of different sizes
% Your code must work for the test case. Also check for different numbers
% in a and b and also different sizes for a and b.
% NO recursion involved in nested function.
Part 2: mergeSort - main function inside which combine2Arrays is written.
% Takes in an unsorted array and returns a sorted array.
% take the input array - split it into two (roughly) equal parts.
% BE CAREFUL: What happens if array size is an odd number?
% Recursively sort these two smaller arrays
% 1. Base case: Think about what would be a "simple" or "trivial" example.
% 2. recursion statement: Call to mergeSort itself.
% Combine these two sorted smaller arrays back using your combine2Arrays
% function.
% We are going to check with any random unsorted array.
% You can use the test case that's given to check whether your code is
% working fine.
% For any input array - you know what the answer is (sorted array).
Homework Problem 2: Merge Sort In this homework problem, you will implement the Merge Sort Algorithm to sort a 1-dimensional double array. Write a nested function called combine2Arrays which takes as input arguments two arrays- arrayl and array2 - and returns an output argument combinedArray. array1 must be a 1xn sorted double array, and array2 must be a 1xm sorted double array. The function should return a combined array which is also sorted. Your code should be able to reproduce the following test case: >> a - [1 3 5]; >> combine2Arrays (a,b) ans - 1 2 3 4 5 Write a function called mergeSort that takes in an argument doubleArray and returns an output argument called sortedArray. doubleArray is a 1xn unsorted double array and sortedArray is the sorted version of doubleArray. In order to achieve merge sort, you will split the array into two sub-arrays of roughly equal size, recursively sort them, and then combine the two sorted sub-arrays. For example, if doubleArray is [10,8,3,8,6,1] then your code should split this into two sub-arrays: [10,8,3] and [8,6, Recursively sorting the sub-arrays should return [3,8,10] and [1,6,8]. Then your code should combine these using the combine2Arrays function that you wrote to return the sortedArray [1,3,6,8,8,10 You can test your code by supply an arbitrary unsorted array doubleArray to mergeSort, and checking that the output is indeed the correct sorted version of the array. Store the code of this problem in the filename mergeSort.m This file must go into your zipped folder as it will be evaluated Homework Problem 2: Merge Sort In this homework problem, you will implement the Merge Sort Algorithm to sort a 1-dimensional double array. Write a nested function called combine2Arrays which takes as input arguments two arrays- arrayl and array2 - and returns an output argument combinedArray. array1 must be a 1xn sorted double array, and array2 must be a 1xm sorted double array. The function should return a combined array which is also sorted. Your code should be able to reproduce the following test case: >> a - [1 3 5]; >> combine2Arrays (a,b) ans - 1 2 3 4 5 Write a function called mergeSort that takes in an argument doubleArray and returns an output argument called sortedArray. doubleArray is a 1xn unsorted double array and sortedArray is the sorted version of doubleArray. In order to achieve merge sort, you will split the array into two sub-arrays of roughly equal size, recursively sort them, and then combine the two sorted sub-arrays. For example, if doubleArray is [10,8,3,8,6,1] then your code should split this into two sub-arrays: [10,8,3] and [8,6, Recursively sorting the sub-arrays should return [3,8,10] and [1,6,8]. Then your code should combine these using the combine2Arrays function that you wrote to return the sortedArray [1,3,6,8,8,10 You can test your code by supply an arbitrary unsorted array doubleArray to mergeSort, and checking that the output is indeed the correct sorted version of the array. Store the code of this problem in the filename mergeSort.m This file must go into your zipped folder as it will be evaluated
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
