mardi 11 juillet 2017

Printing sorted and unsorted list in python

I have a code which generates a list of numbers and then sorts it. The sorting function works but I need to print the unsorted list after sorting. That's why I put the print function to print the unsorted but instead it prints the sorted list. I thought I stored the unsorted list by assigning the unsorted list to a variable and then try to print it in the end. Any ideas why it prints the sorted list instead of the unsorted? I didn't put the GenerateNumbers() function as what is does is give the "numbers" variable a list of unsorted numbers.I'm using insertion sort.

def InsertionSort(sort_list):
    print("Sorting numbers...")
    for i in range(0, len(sort_list)-1):
       for j in range(i+1,0,-1):
         if(sort_list[j] < sort_list[j-1]):
             temp = sort_list[j-1]
             sort_list[j-1] = sort_list[j]
             sort_list[j] = temp
             print(sort_list)
          else:
             break
    print("Finished sorting.")
    print()
    return sort_list

numbers = GenerateNumbers()
unsorted = numbers
sort_list = numbers

sorted_numbers = InsertionSort(sort_list)
print("unsorted list:", numbers)
print("sorted list:", sorted_numbers)




Aucun commentaire:

Enregistrer un commentaire