Python implementation bubble, insert, select Sort Simple instance

Source: Internet
Author: User
The python implementation described in this article is bubbling, inserting, selecting a simple instance of sort for Python beginners to learn from the basics of data structures and algorithms, examples are simple to understand, the code is as follows:

#-*-coding:cp936-*-#python插入排序def Insertsort (a): for  I in range (Len (a)-1):    #print a,i for     J in range (i+1,l En (a)):      if A[I]>A[J]:        temp = a[i]        a[i] = a[j]        a[j] = temp  return A#python bubble sort  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#python selection Sort def selectionsort (alist): 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)

Interested friends can try to test the examples of this article, I believe there will be a new harvest.

  • 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.