def bubble_sort(x):
n = len(x)
for i in range(n-1, 0, -1):
for j in range(i):
if x[j] > x[j+1]:
x[j], x[j+1] = x[j+1], x[j]
return(x)
x = [9,3,6,1,7,10,0]
print(bubble_sort(x))
[0, 1, 3, 6, 7, 9, 10]

How may I rewrite it as a method so that I can get:

Thank you very much Steven. I am aware that there is a built-in sort method, and my question is a data structure question. I tried to modified my code accordingly and get the results.

Is it common to have a class without initialization â€śdef init(â€ť ? In this case, you define MyList as a class based on a list.

class MyList(list):
def bubble_sort3(self):
n = len(self)
for i in range(n - 1, 0, -1):
for j in range(i):
if self[j] > self[j + 1]:
self[j], self[j + 1] = self[j + 1], self[j]
L = MyList([9, 3, 6, 1, 7, 10, 0])
L.bubble_sort3()
print('L\n')
print(L)
L
[0, 1, 3, 6, 7, 9, 10]

Yes. Not providing an __init__ just means it uses the init of the
superclass, list. Thatâ€™s fine. All youâ€™re doing is making a subclass
with one extra method - everything else comes from the superclass.

Thank you very much, Cameron and Steven. I was wondering if itâ€™s possible to write merge sort by class method rather than a function? Is it appropriate to do so? Any comments on the code are welcome.

# merge function for mergesort
def merge_sorted(x, y): # assume that the two sequences are sorted
res = []
while x and y:
if x[0] <= y[0]:
res += [x[0]]
x.pop(0)
else:
res += [y[0]]
y.pop(0)
if x:
return res + x
else:
return res + y
def mergesort(x): # assume that x is an non-empty list
n = len(x)
if n == 1:
return x
elif n == 2:
return [min(x), max(x)]
else: # n == 3 or n ==4:
first_half = x[: n//2]
second_half = x[n//2:]
first_half_sorted = mergesort(first_half)
second_half_sorted = mergesort(second_half)
return merge_sorted(first_half_sorted, second_half_sorted)
import numpy as np
listx = list(np.random.permutation(10))
print(listx)
print('\n')
print(mergesort(listx))

Well, you can definitely do it with a subclass as in your own previous
example.

The difficulty with a class method (no, not a â€ś@classmethodâ€ť class
method, I mean an â€śinstance methodâ€ť - you mean this also) is getting it
into the class you want it to work on.

The subclass approach means you need to work against instances of the
subclass. Using your MyList as an example, that works fine, but breaks
as soon as regular lists get mixed in, as they lack the method. You end
up needing to have convenient factories to convert ordinary lists into
the subclass in order to use the method. That quite possibly involves
copying the list (which is expensive) and also directly breaks the â€śsort
in placeâ€ť aspect of your example. And it also becomes wordy and
cumbersome very quickly.

You can define a â€śmixinâ€ť class (a bare class with just the extra
methods) and include it in any subclasses:

class SortableMixin:
def mergesort(self):
... in place merge sort ...

and easily mix that into any subclasses:

class MyList(list, SortableMixin):
pass
class SparseList(SortableMixin):
... special "sparse" list implementation of a list ...

but youâ€™re still subclassing. I do not know if you can modify a classâ€™s
method resolution order (the __mro__ attribute). [â€¦ experimentsâ€¦]
No you cannot.

Anyway, the advantage of a function is that you can apply it to any
type of object (provided the object is suitable). So your mergesort
function can be applied to a list or the a MyList or any other
collection which looks enough like a list.

So a function can often be a good way to do things, particularly since
it isnâ€™t bound to a specific class.