2014-12-14 4 views
-1

재귀 선택 정렬을 쓰려고하는데, 정말 혼란 스럽습니다. 왜 이것이 작동하지 않는지 추적하기가 정말 어렵습니다. 누군가가 문제가 어디에 있는지 말해 줄 수 있다면 좋을 것입니다!재귀 선택 정렬

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 

답변

0

입니다.

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); 
    } 
0

이 시도 :

다음은이 수정을 시도하십시오 내 코드

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