Python implementation bubble, insert, select sort Simple instance _python

Source: Internet
Author: User

The Python implementation described in this article bubbles, inserts, and selects simple instances of sorting that are more suitable for Python beginners to learn from the basics of data structures and algorithms, the examples are simple and understandable, the specific code is as follows:

#-*-coding:cp936-*-
#python插入排序
def insertsort (a): for
  I in range (Len (a)-1):
    #print a,i to 
    J in R Ange (I+1,len (a)):
      if A[I]>A[J]:
        temp = a[i]
        a[i] = a[j]
        a[j] = temp return
  a

#Python的冒泡排序  
def bubblesort (alist): for
  passnum in range (len (alist) -1,0,-1):
    #print alist,passnum to
    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的选择排序 
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 
  alist

alist = [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 test the example 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.