我正在 MATLAB 中编写插入排序。我这样调用我的函数:
>> A = [5 4 3 2 1]
A =
5 4 3 2 1
>> insertion_sort(A)
但是当我运行它时,我得到了错误
??? Attempt to reference field of non-structure array.
Error in ==> insertion_sort at 6
for j=2:original.length
这是我的原始代码:
function sorted = insertion_sort(original)
for j=2:original.length
key = original(j);
i = j-1;
while i > 0 && original(i) > key
original(i+1) = original(i);
i = i-1;
end
original(i+1) = key;
end
sorted = original;
end
有人知道我在做什么错吗?