Question

In: Computer Science

IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...

IN MATLAB!!

Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order

Solutions

Expert Solution

MATLAB Code:

File: MergeSort.m

function sortedArray = MergeSort(arr)
% Function that sorts the array using MergeSort
len = length(arr);
  
% Checking length
if len <= 1
% Storing sorted array
sortedArray = arr;
else
% Calling merge function + Recursive Call
sortedArray = Merge(MergeSort(arr(1:round(len/2))), MergeSort(arr((round(len/2)+1):len)));
end % If end
end % Function end

File: Merge.m

function mArr = Merge(arr1, arr2)
% Helper function for MergeSort
% Calculatint total length
len = length(arr1) + length(arr2);
  
% Filling with zeros
mArr = zeros(1, len);
  
% Creating arrays
arr1 = [arr1, inf];
arr2 = [arr2, inf];
  
% Index values
idxArr1 = 1; idxArr2 = 1;
  
% Looping over array
for i = 1:len
% Comparing elements
if arr1(idxArr1) < arr2(idxArr2)
% Updating arrays
mArr(i) = arr1(idxArr1);
% Incrementing index
idxArr1 = idxArr1 + 1;
else
% Updating arrays
mArr(i) = arr2(idxArr2);
% Incrementing index
idxArr2 = idxArr2 + 1;
end % If end
end % for end
  
end % function end

File: mergeSortScript.m

% Script that implements MergeSort

% Creating a 1D array
arr = [95, 57, 35, 37, 3, 12];

fprintf("\nBefore Sort: ");
disp(arr);

% Calling MergeSort function
arr = MergeSort(arr);

fprintf("\nAfter Sort: ");
disp(arr);

______________________________________________________________________________________

Sample Run:


Related Solutions

Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
24.10 Lab Ch24: MergeSortDouble Create a class MergeSortDouble and implement a sort(double[]) method using merge sort...
24.10 Lab Ch24: MergeSortDouble Create a class MergeSortDouble and implement a sort(double[]) method using merge sort algorithm. Test will call the sort() method. java please!
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. C++ program thanks
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
In MATLAB Write a function to create an array of N numbers with a normal distribution....
In MATLAB Write a function to create an array of N numbers with a normal distribution. Call that from a script to create 1000 numbers, with a mean of 50 and a sigma of 10.
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT