冒泡排序

#数组的情况
a=[2,4,1,3,6,77,23,235,6756,12,34,322,22,14,52,21,24,54,63,212]
for i in range(0,len(a)):
    for j in range(i+1,len(a)):
        one=a[i]
        two=a[j]
        if one > two:
            a[j]=one
            a[i]=two
print a


#字符串的情况
string='asd280j1-?><u!@#6oi7f'
for i in range(0,len(string)):
    for j in range(i+1,len(string)):
        if j==len(string):
            break
        one=string[i]
        two=string[j]
        if ord(one)>ord(two):
            string=string[:i]+two+string[i+1:j]+one+string[j+1:]
print string

Notice!
尝试修改string的值会导致“TypeError: 'str' object does not support item assignment
string是一种不可变的数据类型,该错误发生在如下代码中:
spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
而你实际想要这样做:
spam = 'I have a pet cat.'
spam = spam[:13] + 'r' + spam[14:]
print(spam)