Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 递归选择排序_Sorting_Recursion_Selection Sort - Fatal编程技术网

Sorting 递归选择排序

Sorting 递归选择排序,sorting,recursion,selection-sort,Sorting,Recursion,Selection Sort,我正在尝试编写一个递归选择排序,我真的很困惑,很难理解为什么它不起作用。如果有人能告诉我问题出在哪里,那就太好了 这是我的密码 def selectionSortRecursive(lis, minIndex = 0): if minIndex - 1 == len(lis): return lis minValueIndex = minIndex #Assigns the very first item in the list as the minimum va

我正在尝试编写一个递归选择排序,我真的很困惑,很难理解为什么它不起作用。如果有人能告诉我问题出在哪里,那就太好了

这是我的密码

def selectionSortRecursive(lis, minIndex = 0):
    if minIndex - 1 == len(lis):
        return lis
    minValueIndex = minIndex #Assigns the very first item in the list as the minimum value index
    for i in range (minIndex + 1, len(lis)):
        if lis[i] < lis[minValueIndex]: #if any item is less than min value, its index gets assigned the minimum value
            minValueIndex = i
    lis[minIndex], lis[minValueIndex] = lis[minValueIndex], lis[minIndex] #After you go through the list, you switch the smallest item into the minimum index, which starts off being 0
    lis = selectionSortRecursive(lis, minIndex+1) #now we're gonna sort the list at the next min
    return lis
def selectionsorrecursive(lis,minIndex=0):
如果minIndex-1==len(lis):
返回lis
minValueIndex=minIndex#将列表中的第一项指定为最小值索引
对于范围内的i(最小索引+1,len(lis)):
如果lis[i]
请尝试此修改:

def selectionSortRecursive(lis, minIndex = 0):
    if minIndex - 1 == len(lis):
        return lis
    minValueIndex = minIndex #Assigns the very first item in the list as the minimum value index
    for i in range (minIndex + 1, len(lis)):
        if lis[i] < lis[minValueIndex]: #if any item is less than min value, its index gets assigned the minimum value
            lis[minValueIndex], lis[i] = lis[i], lis[minValueIndex] #After you go through the list, you switch the smallest item into the minimum index, which starts off being 0
    lis = selectionSortRecursive(lis, minIndex+1) #now we're gonna sort the list at the next min
    return lis
def selectionsorrecursive(lis,minIndex=0):
如果minIndex-1==len(lis):
返回lis
minValueIndex=minIndex#将列表中的第一项指定为最小值索引
对于范围内的i(最小索引+1,len(lis)):
如果lis[i]
试试这个

static int maxIndex;
    public static void selectionSort(Object[] source, int fromIndex, int endIndex){
        if(endIndex==fromIndex){
            return;
        }
        else{
            if(((Comparable) source[fromIndex]).compareTo(source [maxIndex])>0){
                maxIndex=fromIndex;
            }
            bubbleSort(source,fromIndex+1,endIndex);
        }
        Object temp=source[fromIndex];
        source[fromIndex]=source[maxIndex];
        source[maxIndex]=temp;
        bubbleSort(source,fromIndex,endIndex-1);
    }