This article example describes Python sorting search basic algorithm of bubble sort. Shared for your reference, as follows:
The bubbling sort is similar to the selection sort in that it is also the nth time that the smallest element is placed in the nth position, which is also the absolute position of that element, except that the process of bubbling sort gradually closes in on the other elements to their final positions. The code is as follows:
def bubbleSort(seq): length=len(seq) for i in range(length): for j in range(length-1,i,-1): if seq[j-1]>seq[j]: seq[j-1],seq[j]=seq[j],seq[j-1] if __name__=='__main__': print("I test results:") seq=[2,9,7,7,4,3,2,-4,54,-7,0] bubbleSort(seq) print(seq)
Run results:
PS: Here is another demo tool on sorting for your reference:
Online animated demonstration of insertion/selection/bubbling/merging/hill/fast sort algorithm process tool:
http://tools./aideddesign/paixu_ys
Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python encryption and decryption algorithms and techniques》、《Summary of Python coding manipulation techniques》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.