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

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)
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.
(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)
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome 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. I need this in java language.
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome 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. I need this in java language. Radix Sort assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT