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)
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.
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...
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.
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT