This section briefly introduces how to use bubble, select, and insert sorting in Python. it is recommended to your partners for reference. I recently learned the basics of python and wrote about the following three sorting trainer skills:
The code is as follows:
'''
Created on
@ Author: codegeek
'''
// Bubble sort
Def bubble_sort (seq ):
For I in range (len (seq )):
For j in range (I, len (seq )):
If seq [j] <seq [I]:
Tmp = seq [j]
Seq [j] = seq [I]
Seq [I] = tmp
// Select sorting
Def selection_sort (seq ):
For I in range (len (seq )):
Position = I
For j in range (I, len (seq )):
If seq [position]> seq [j]:
Position = j
If position! = I:
Tmp = seq [position]
Seq [position] = seq [I]
Seq [I] = tmp
// Insert sorting
Def insertion_sort (seq ):
If len (seq)> 1:
For I in range (1, len (seq )):
While I> 0 and seq [I] <seq [i-1]:
Tmp = seq [I]
Seq [I] = seq [i-1]
Sequence [i-1] = tmp
I = I-1
//
If _ name _ = "_ main __":
Print "-------- bubble_sort -------------"
Seq = [,]
Bubble_sort (seq)
Print seq
Print "-------- selection_sort -------------"
Seq = [88,44, 33,4, 7,6, 8,9, 11]
Selection_sort (seq)
Print seq
Print "-------- insertion_sort -------------"
Seq = [777,44, 33,4, 7,6, 1111,100, 11]
Insertion_sort (seq)
Print seq
The above are three bubbles in Python: Select, insert the sorting code and use method. I hope my friends will like it.