[Python-algorithm] Python implements bubble, insert, and select sort

Source: Internet
Author: User

I have never systematically studied data structures and algorithms from the beginning.

Partial reference: http://interactivepython.org/courselib/static/pythonds/SortSearch/sorting.html

#-*-Coding: cp936-*-# insert sort def insertsort (a): for I in range (LEN (a)-1): # print, I For J in range (I + 1, Len (a): If a [I]> A [J]: temp = A [I] a [I] = A [J] a [J] = temp return a # Python's Bubble Sorting def bubblesort (alist ): for passnum in range (LEN (alist)-1, 0,-1): # print alist, passnum for I in range (passnum ): if alist [I]> alist [I + 1]: temp = alist [I] alist [I] = alist [I + 1] alist [I + 1] = temp return alist # select and sort def selectionsort (alist) in Python ): for I in range (LEN (alist)-1, 0,-1): maxone = 0 for J in range (1, I + 1 ): if alist [J]> alist [maxone]: maxone = J temp = alist [I] alist [I] = alist [maxone] alist [maxone] = temp return alistalist = [54,26, 93,17, 77,31, 44,55, 20] # print bubblesort (alist) alist = [54,26, 93,17, 77,31, 44,55, 20] print selectionsort (alist)

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.