Error with mutable function to code merge sort

4 次查看(过去 30 天)
Hello,
I try to code a merge sort in MATLAB, I successed but to improve performance, I want like Julia, use mutable function to avoid allocations. I wonder if are mutables functions exist in MATLAB ? This is my code but it yields the same array...
function mergeMutable(A)
Aux = A;
merge(Aux, A, 1, length(A));
end
function merge(A, Aux, l, r)
if r > l
m = floor((l + r) / 2);
merge(Aux, A, l, m);
merge(Aux, A, m + 1, r);
i = l;
j = m + 1;
for k = l:r
if i > m
Aux(k) = A(j);
j = j + 1;
elseif j > r
Aux(k) = A(i);
i = i + 1;
elseif A(j) < A(i)
Aux(k) = A(j);
j = j + 1;
else
Aux(k) = A(i);
i = i + 1;
end
end
end
end
I write too the Julia code (which works) even it's almost the same syntax :
function merge!(A, Aux, l, r)
if (r > l)
m = (l+r) ÷ 2
merge!(Aux, A, l, m)
merge!(Aux, A, m + 1, r)
i = l
j = m + 1
@inbounds for k in l:r
if i > m
Aux[k] = A[j]
j += 1
elseif j > r
Aux[k] = A[i]
i += 1
elseif A[j] < A[i]
Aux[k] = A[j]
j += 1
else
Aux[k] = A[i]
i += 1
end
end
end
end
function mergeMutable!(A)
Aux = copy(A)
merge!(Aux, A, 1, length(A))
end
Thank you !

回答(2 个)

Mahesh Chilla
Mahesh Chilla 2023-7-8
编辑:Mahesh Chilla 2023-7-8
Hi Raphael!
The provided MATLAB code implements the merge sort algorithm, which is a recursive sorting algorithm. It starts by dividing the array into smaller halves until individual elements are reached. Then, it merges the sorted halves to create a fully sorted array. The 'mergeMutable' function is the main entry point that calls the 'merge' function to sort the array A. The 'merge' function performs the actual merging process by comparing elements from the divided halves and placing them in the correct order. It uses indices to keep track of the positions in the left, right, and merged arrays. After merging, any remaining elements are copied to the merged array. Finally, the sorted portion of the array 'Aux' is returned.
A = [3, 2, 1, 4, 5, 8, 7, 6];
mergeMutable(A);
1 2 3 4 5 6 7 8
function mergeMutable(A)
Aux = merge(A, 1, length(A));
disp(Aux); % Display the sorted array
end
function Aux = merge(A, l, r)
if r > l
m = floor((l + r) / 2); % Calculate the middle index
% Recursively call merge on the left and right halves
left = merge(A, l, m);
right = merge(A, m + 1, r);
i = 1; % Starting index for the left array
j = 1; % Starting index for the right array
k = l; % Starting index for the merged array
% Merge the left and right arrays in sorted order
while i <= length(left) && j <= length(right)
if left(i) <= right(j)
A(k) = left(i); % Place the smaller element in the merged array
i = i + 1; % Move to the next element in the left array
else
A(k) = right(j); % Place the smaller element in the merged array
j = j + 1; % Move to the next element in the right array
end
k = k + 1; % Move to the next position in the merged array
end
% Copy any remaining elements from the left array
while i <= length(left)
A(k) = left(i);
i = i + 1;
k = k + 1;
end
% Copy any remaining elements from the right array
while j <= length(right)
A(k) = right(j);
j = j + 1;
k = k + 1;
end
end
Aux = A(l:r); % Return the sorted portion of the array
end
In MATLAB, you can define classes with mutable properties, and then define methods that modify those properties. By using mutable objects and methods, you can achieve similar functionality to mutable functions in Julia.
To learn more about the mutable and immutable properties, refer the MATLAB's documentation.
Hope this helps,
Thank you!!

Walter Roberson
Walter Roberson 2023-7-8
On MATLAB, when you modify an input parameter, the change is local and does not affect the calling environment unless the new value is returned and the caller assigns overtop of the existing variable.

类别

Help CenterFile Exchange 中查找有关 Formatting and Annotation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by