python選擇排序算法實(shí)例總結(jié)
本文實(shí)例總結(jié)了python選擇排序算法。分享給大家供大家參考。具體如下:
代碼1:
def ssort(V): #V is the list to be sorted j = 0 #j is the "current" ordered position, starting with the first one in the list while j != len(V): #this is the replacing that ends when it reaches the end of the list for i in range(j, len(V)): #here it replaces the minor value that it finds with j position if V[i] < V[j]: #but it does it for every value minor than position j V[j],V[i] = V[i],V[j] j = j+1 #and here's the addiction that limits the verification to only the next values return V
代碼2:
def selection_sort(list): l=list[:] # create a copy of the list sorted=[] # this new list will hold the results while len(l): # while there are elements to sort... lowest=l[0] # create a variable to identify lowest for x in l: # and check every item in the list... if x<lowest: # to see if it might be lower. lowest=x sorted.append(lowest) # add the lowest one to the new list l.remove(lowest) # and delete it from the old one return sorted
代碼3
a=input("Enter the length of the list :") # too ask the user length of the list l=[] # take a emty list for g in range (a): # for append the values from user b=input("Enter the element :") # to ask the user to give list values l.append(b) # to append a values in a empty list l print "The given eliments list is",l for i in range (len(l)): # to repeat the loop take length of l index=i # to store the values i in string index num=l[i] # to take first value in list and store in num for j in range(i+1,len(l)): # to find out the small value in a list read all values if num>l[j]: # to compare two values which store in num and list index=j # to store the small value of the loop j in index num=l[j] # to store small charecter are value in num tem=l[i] # to swap the list take the temparary list stor list vlaues l[i]=l[index] # to take first value as another l[index]=tem print "After the swping the list by selection sort is",l
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python3時(shí)間轉(zhuǎn)換之時(shí)間戳轉(zhuǎn)換為指定格式的日期方法詳解
這篇文章主要介紹了Python3時(shí)間轉(zhuǎn)換之時(shí)間戳轉(zhuǎn)換為指定格式的日期,需要的朋友可以參考下2021-04-04pytorch訓(xùn)練時(shí)的顯存占用遞增的問(wèn)題解決
本文主要介紹了pytorch訓(xùn)練時(shí)的顯存占用遞增的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Python中的JSON?Pickle?Shelve模塊特性與區(qū)別實(shí)例探究
在Python中,處理數(shù)據(jù)序列化和持久化是極其重要的,JSON、Pickle和Shelve是三種常用的模塊,它們提供了不同的方法來(lái)處理數(shù)據(jù)的序列化和持久化,本文將深入研究這三個(gè)模塊,探討它們的特性、用法以及各自的優(yōu)缺點(diǎn)2024-01-01Python Playwright安裝和基本使用問(wèn)題記錄
這篇文章主要介紹了Playwright安裝和基本使用問(wèn)題記錄,playwright是一款新型的自動(dòng)化測(cè)試工具,功能非常強(qiáng)大,有很多優(yōu)點(diǎn),缺點(diǎn)就是使用的人比較少,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05使用python繪制人人網(wǎng)好友關(guān)系圖示例
這篇文章主要介紹了使用python繪制人人網(wǎng)好友關(guān)系圖示例,需要的朋友可以參考下2014-04-04如何解決django配置settings時(shí)遇到Could not import settings ''conf.loca
這里記錄一下在項(xiàng)目中遇到django配置settings時(shí)遇到Could not import settings 'conf.local'的解決方法,有同樣問(wèn)題的小伙伴們參考下吧2014-11-11Python字符串大小寫(xiě)轉(zhuǎn)換拼接刪除空白
這篇文章主要介紹了Python字符串大小寫(xiě)轉(zhuǎn)換拼接刪除空白的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09python發(fā)送json參數(shù)的實(shí)例代碼
在寫(xiě)腳本的過(guò)程中,除了發(fā)送form表單參數(shù)之外,我們還會(huì)發(fā)送json格式的參數(shù)。那么碰見(jiàn)json格式要怎么發(fā)送呢,這篇我們來(lái)解決這個(gè)問(wèn)題,需要的朋友可以參考下2019-10-10