1

I have this python code here below (for bubble sort). Below that is my attempt at converting it to MATLAB code. I'm new to MATLAB, and I'm doing the conversion for practice. I would appreciate feedback on how accurate/incorrect my conversion is.

The python version:

def bubble_sort(alist):
    return bubble_sort_helper(alist, len(alist))
def bubble_sort_helper(alist, n):
    if n < 2:
        return alist
    for i in range(len(alist)-1):
        if alist[i] > alist[i+1]:
            temp = alist[i]
            alist[i] = alist[i+1]
            alist[i+1] = temp
    return bubble_sort_helper(alist, n-1)

My attempt at a MATLAB conversion:

function a = bubble_sort(alist)
    a = bubble_sort_helper(alist, size(alist))
end

function b = bubble_sort_helper(alist, n)
    if n < 2
        b = alist
    end
    for ii = size(alist)
        if alist(1) > alist (ii+1)
            temp = alist(ii)
            alist(ii) = alist(ii+1)
            alist(ii+1) = temp
        end
    end
    b = bubble_sort_helper(alistn n-1)

end
5
  • I forgot to add the indent under the if statement for the python code. Fixed that with an edit, my mistake. Commented Aug 11, 2016 at 19:12
  • 2
    Did you test to see if it sorted your input? Commented Aug 11, 2016 at 19:20
  • 1) Does it work as expected? 2) Why is it recursive? Commented Aug 11, 2016 at 19:22
  • 1
    note that in your python code you could do alist[i], alist[i+1] = alist[i+1], alist[i] Commented Aug 11, 2016 at 19:26
  • @Suever, I ran the python code and it works, but my original MATLAB code posted above does not run. Commented Aug 11, 2016 at 19:33

2 Answers 2

2

A few issues here:

  1. You need to use numel rather than size to get the number of elements in an array. size will give you a vector of sizes of each dimension and numel will give you the total number of elements

  2. You need to actually create an array of values for your for loop to loop through. To do this, use the colon to create an array from 2 to n.

    for ii = 2:n
    end
    
  3. You use ii as your looping variable but try to use i inside of the loop. Pick one and stick to it (preferably not i)

  4. To flip the values you can simply do your assignment like this:

    alist([i-1, i]) = alist([i, i-1]);
    

Taken together, this will give you something like this:

function a = bubble_sort(alist)
    a = bubble_sort_helper(alist, numel(alist))
end

function b = bubble_sort_helper(alist, n)
    if n < 2
        b = alist;
    else
        for k = 2:n
            if alist(k-1) > alist(k)
                alist([k-1, k]) = alist([k, k-1]);
            end
        end
        b = bubble_sort_helper(alist, n-1);
    end
end
Sign up to request clarification or add additional context in comments.

Comments

1

Your python version is inefficient:

def bubble_sort(alist):
    return bubble_sort_helper(alist, len(alist))
def bubble_sort_helper(alist, n):
    if n < 2:
        return alist
    for i in range(n-1):
        if alist[i] > alist[i+1]:
            alist[i], alist[i+1] = alist[i+1], alist[i]
    return bubble_sort_helper(alist, n-1)

and your matlab code is wrong:

function a = bubble_sort(alist)
    a = bubble_sort_helper(alist, size(alist))
end

function b = bubble_sort_helper(alist, n)
    if n < 2
        b = alist;
    else
        for i = 2:n
            if alist(i-1) > alist(i)
                temp = alist(i-1);
                alist(i-1) = alist(i);
                alist(i) = temp;
            end
        end
        b = bubble_sort_helper(alist, n-1);
    end
end

1 Comment

Ohh, thanks for the tip about my inefficiency. I'm trying to learn all I can about sorting algorithms so every bit helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.