Python Selection Sort--Does not appear to be working

python

#1

Here is some test code that I am unable to get to print the sorted list that was passed to a function.

Any help on what I am doing wrong or missing would be greatly appreciated!

# This program will send a list from the main function to a sort function
# that will sort the list of names to be displayed.
# Output will be to Print list before and after sort.

def main():
    names = ['Jean', 'Jose', 'Joseph', 'Jane', 'Janeen']
    print (names)
    selection_sort (names)

    
    
# The selection_sort function performs the selection sort.
def selection_sort(testArray):
# Set start_scan to 0 for OUTER LOOP.     
    start_scan = 0
    while start_scan < len(testArray) - 1:
        min_index = start_scan
        min_value = testArray[start_scan]
        
# Initialize index for the inner loop starting at the 2nd element.
        index = start_scan + 1
        
        while index < len(testArray):
            if testArray[index] < min_value:
                min_value = testArray[index]
                min_index = index

# Increment index.
                index = index + 1

# Swap element w/smallest value w/1st element in the scannable area.
                testArray[min_index] = testArray[start_scan] 
                testArray[start_scan] = min_value

# Increment start_scan.
                start_scan = start_scan + 1

    print (testArray)

main()

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.