This article mainly introduces Python's implementation of bubble, insert, and simple sorting examples. It is suitable for beginners of Python to learn and use for reference. If you need it, you can refer to the Python Implementation of bubble, insert, simple sorting example is suitable for beginners of Python to learn data structures and algorithms from the basics. The sample code is as follows:
#-*-Coding: cp936-*-# insert sort def insertSort (a): for I in range (len (a)-1): # print, I for j in range (I + 1, len (a): if a [I]> a [j]: temp = a [I] a [I] = a [j] a [j] = temp return a # Python's Bubble Sorting def bubbleSort (alist ): for passnum in range (len (alist)-1, 0,-1): # print alist, passnum for I in range (passnum ): if alist [I]> alist [I + 1]: temp = alist [I] alist [I] = alist [I + 1] alist [I + 1] = temp return alist # select and sort def selectionSort (alist) in Python ): for I in range (len (alist)-1, 0,-1): maxone = 0 for j in range (1, I + 1 ): if alist [j]> alist [maxone]: maxone = j temp = alist [I] alist [I] = alist [maxone] alist [maxone] = temp return alistalist = [54,26, 93,17, 77,31, 44,55, 20] # print bubbleSort (alist) alist = [54,26, 93,17, 77,31, 44,55, 20] print selectionSort (alist)
If you are interested, you can test the examples in this article. I believe there will be new gains.