为什么这不是选择排序算法

时间:2020-05-06 12:40:16

标签: python list algorithm selection-sort

我正在尝试执行选择排序算法,但是我无法弄清我缺少什么。 下面给出两个代码 代码1,我尝试使用索引访问值,并且可以正常工作

代码1:

<xdr:wsDr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:xvml="urn:schemas-microsoft-com:office:excel" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:pvml="urn:schemas-microsoft-com:office:powerpoint" xmlns:cppr="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:msink="http://schemas.microsoft.com/ink/2010/main" xmlns:cdr14="http://schemas.microsoft.com/office/drawing/2010/chartDrawing" xmlns:c15="http://schemas.microsoft.com/office/drawing/2012/chart" xmlns:cs="http://schemas.microsoft.com/office/drawing/2012/chartStyle" xmlns:ns38="http://www.w3.org/1998/Math/MathML" xmlns:ns39="http://www.w3.org/2003/InkML" xmlns:a13cmd="http://schemas.microsoft.com/office/drawing/2013/main/command" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:c16="http://schemas.microsoft.com/office/drawing/2014/chart" xmlns:dgm1611="http://schemas.microsoft.com/office/drawing/2016/11/diagram" xmlns:c173="http://schemas.microsoft.com/office/drawing/2017/03/chart" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:an18="http://schemas.microsoft.com/office/drawing/2018/animation" xmlns:anam3d="http://schemas.microsoft.com/office/drawing/2018/animation/model3d" xmlns:iact="http://schemas.microsoft.com/office/powerpoint/2014/inkAction" xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:wetp="http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11" xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" xmlns:comp="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:dgm14="http://schemas.microsoft.com/office/drawing/2010/diagram" xmlns:a15="http://schemas.microsoft.com/office/drawing/2012/main" xmlns:pic14="http://schemas.microsoft.com/office/drawing/2010/picture" xmlns:c16ac="http://schemas.microsoft.com/office/drawing/2014/chart/ac" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" xmlns:a1611="http://schemas.microsoft.com/office/drawing/2016/11/main" xmlns:dgm1612="http://schemas.microsoft.com/office/drawing/2016/12/diagram" xmlns:a16svg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative" xmlns:a18hc="http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor" xmlns:wp15="http://schemas.microsoft.com/office/word/2012/wordprocessingDrawing">

但是代码2,我认为我尝试的基本上是相同的。我正在比较值,然后获取它们的索引并相互交换,但是不知何故数组根本没有变化,我无法弄清楚我缺少了什么

代码2:

arr = [20,11,64,28,80,64,61,58,23,50,55,21,34,76,39,29,15,66,13,91,74,51]
for i in range(len(arr)):
    min_ele = i
    for j in range(i,len(arr)):
        if arr[min_ele] > arr[j]:
            min_ele = j


    arr[i],arr[min_ele] = arr[min_ele],arr[i]

print(arr)

1 个答案:

答案 0 :(得分:0)

index_1 = arr.index(x)
index_2 = arr.index(min_ele)

arr[index_1],arr[index_2] = arr[index_2],arr[index_1]

但是以某种方式有效

相关问题