Python implements a simple list bubble sorting and reverse list operation example

  • 2021-07-13 05:36:33
  • OfStack

In this paper, the example shows that Python implements simple list bubble sorting and reverse list operation. Share it for your reference, as follows:


# -*- coding:utf-8 -*-
#! python2
a=[3,4,6,2,1]
print a
def sortAndAddNew(a):
  b=[]
  l=len(a)
  for i in range(l):
    j=i
    for j in range(l):
      if (a[i]>a[j]):
        a[i],a[j]=a[j],a[i]
  for k in range(len(a)):
    b.append(a[k])
  return b
b=sortAndAddNew(a)
print b
print list(reversed(b))

Run results:

[3, 4, 6, 2, 1]
[6, 4, 3, 2, 1]
[1, 2, 3, 4, 6]

PS: Here we recommend another demonstration tool about sorting for your reference:

Online animation demonstrates insert/select/bubble/merge/hill/quick sort algorithm process tool:
http://tools.ofstack.com/aideddesign/paixu_ys

More readers interested in Python can check the topics of this site: "Python Data Structure and Algorithm Tutorial", "Python List (list) Operation Skills Summary", "Python Coding Operation Skills Summary", "Python Function Use Skills Summary", "Python String Operation Skills Summary" and "Python Introduction and Advanced Classic Tutorial"

I hope this article is helpful to everyone's Python programming.


Related articles: