Inserting the concept of sort and bubbling sort I won't explain.
。。。 Insert Sort Code:
1 if __name__ = = ' __main__ ': 2 L = [4,1,9,13,34,26,10,7,4] 3 4 #=========================insert_sort=========== ============== 5 def insert_sort (L): 6 for I in range (Len (l)): 7 Min_index = i 8 for J in Range (I+1,len (l) ): 9 if l[min_index] > l[j]:10 min_index = j11 tmp = l[i]12 l[i] = l[min_index]13 L[min_index] = TMP14 Print (str (l)) print ("Result:" + str (l))- Insert_sort (l) print ("Insert_sort Success!!! ")
Each traversal, first assume List[min_index] at the lowest value, and then followed by the value of the comparison, when found List[j] than List[min_index] value of the hour, then the Min_index is replaced by J, followed by the comparison, guide to find the smallest list [j], Will J pay Min_index, then L[min_index] is the minimum value of the Traverse process, do not know to say clearly no ~ ~
The results are as follows:
[1, 4, 9, 13, 34, 26, 10, 7, 4] [1, 4, 9, 13, 34, 26, 10, 7, 4] [1, 4, 4, 13, 34, 26, 10, 7, 9] [1, 4, 4, 7, 34, 26, 10, 13, 9] [1, 4, 4, 7, 9, 26, 10, 13, 34] [1, 4, 4, 7, 9, 10, 26, 13, 34] [1, 4, 4, 7, 9, 10, 13, 26, 34] [1, 4, 4, 7, 9, 10, 13, 26, 34] [1, 4, 4, 7, 9, ten,, 34]result: [1, 4, 4, 7, 9, ten,, 34]insert_sort success!!!
Here's another look at bubble sort
1 #==========================bubble_sort======================== 2 3 def bubble_sort (L): 4 for I in range (Len (L), 0,-1): 5 for J in Range (Len (l)-1): 6 if L[J] > l[j+1]: 7 tmp = L[j] 8 l[j] = l[j+1] 9 l[j+1 ] = tmp10 print ("Result:" + str (l)) Bubble_sort (L) print ("Bubble_sort success!!!")
Don't explain it.
Insert sort and bubble sort with python