Reputation: 2357
As an intellectual challenge, I'm trying to do what is described in the title. I am stuck, and wondering if anyone had any helping ideas..?
def main():
my_list = [1,2,3,4,5,6,3,4,63,3]
sort(my_list)
def sort(my_list):
n=0
m=1
for number in my_list:
if my_list[n] < my_list[m]:
my_list[n] = my_list[n+1]
n+=1
m+=1
print my_list
if __name__ == '__main__':
main()
Upvotes: 2
Views: 5852
Reputation: 41
def sort(L):
newlist = []
for x in range(len(L)):
newlist.append(min(L))
L.remove(newlist[x])
return newlist
Upvotes: 0
Reputation: 635
There are many sorting algorithms, such as bubble
, quick sort
and so on, and here is a reference http://www.sorting-algorithms.com/, you can implement any of them according to the descriptioin.
The following is bubble sort
def main():
my_list = [1,2,3,4,5,6,3,4,63,3]
sort(my_list)
def sort(my_list):
size = len(my_list)
for i in range(size):
for j in range(size-i-1):
if(my_list[j] > my_list[j+1]):
tmp = my_list[j]
my_list[j] = my_list[j+1]
my_list[j+1] = tmp
print my_list
if __name__ == '__main__':
main()
Hope helps!
Upvotes: 3
Reputation: 309899
Most python implementations use the mergesort
algorithm1.
A quick google search will turn up an implementation, but the algorithm is simple enough that it's worth trying to code it yourself.
1 (Actually, they use Tim sort which is a hybrid between mergesort and insertion sort ...)
Upvotes: 1