欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)冒泡,插入,選擇排序簡單實(shí)例

 更新時(shí)間:2014年08月18日 11:23:41   投稿:shichen2014  
這篇文章主要介紹了Python實(shí)現(xiàn)冒泡,插入,選擇排序簡單實(shí)例,很適合Python初學(xué)者學(xué)習(xí)參考之用,需要的朋友可以參考下

本文所述的Python實(shí)現(xiàn)冒泡,插入,選擇排序簡單實(shí)例比較適合Python初學(xué)者從基礎(chǔ)開始學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)和算法,示例簡單易懂,具體代碼如下:

# -*- coding: cp936 -*-
#python插入排序
def insertSort(a):
  for i in range(len(a)-1):
    #print a,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的冒泡排序  
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的選擇排序 
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)

感興趣的朋友可以動(dòng)手測試一下本文實(shí)例,相信會有新的收獲。

相關(guān)文章

最新評論