# Sort.py Copyright (c) (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-23 File created. # 2022-12-16 Converted to Python 3. # The sort_to_ascending_order() function in this module is # called from SortMain.py # Note that this program is a 'translated' version of the # corresponding Java program. These functions should not be widely # used to sort Python lists. See the comment at the end of # SortMain.py def get_index_of_smallest_number_in_list( list_of_numbers, index_of_first_number_to_process, number_of_numbers_to_process ) : smallest_number = list_of_numbers[ index_of_first_number_to_process ] index_of_smallest_number = index_of_first_number_to_process number_index = index_of_first_number_to_process + 1 while number_index < index_of_first_number_to_process + \ number_of_numbers_to_process : if list_of_numbers[ number_index ] < smallest_number : smallest_number = list_of_numbers[ number_index ] index_of_smallest_number = number_index number_index += 1 return index_of_smallest_number def sort_to_ascending_order( list_of_numbers, number_of_numbers_in_list ) : for number_index in range( number_of_numbers_in_list ) : number_of_unsorted_numbers = \ number_of_numbers_in_list - number_index index_of_smallest_number = \ get_index_of_smallest_number_in_list( list_of_numbers, number_index, number_of_unsorted_numbers ) smallest_number = list_of_numbers[ index_of_smallest_number ] # Now we simply put the number in current list # position to where the smallest number is, and # then put the smallest number in current position. list_of_numbers[ index_of_smallest_number ] = \ list_of_numbers[ number_index ] list_of_numbers[ number_index ] = smallest_number