Insert sort and bubble sort with python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.