Recently learned the basics of Python, write a 3 sort of practice practiced hand:
Copy the Code code as follows:
'''
Created on 2013-8-23
@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 sort
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 Sort
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]
SEQ[I-1] = tmp
i = I-1
//
if __name__ = = "__main__":
Print "--------bubble_sort-------------"
SEQ = [22,1,33,4,7,6,8,9,11]
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 is the 3 Python bubble, select, insert the sort of code and use the method, I hope that the small partners can enjoy.