selection-sort

How to implement Selection Sort within a list?

筅森魡賤 提交于 2019-12-02 02:59:01
So I've got a .txt file as follows: 131,263.07 47,170.14 170,190.01 180,412.69 53,401.53 And I had to read the file so as to output the list like: 131 kms, $263.07 47 kms, $170.14 170 kms, $190.01 180 kms, $412.69 53 kms, $401.53 The code I used was: def PrintList(table): for line in table: print(str(line[0]) + " kms, $" + str(line[1])) file = open(input("Enter file name: ")) table = [] for line in file: line = line.rstrip().split(",") line[0] = int(line[0]) line[1] = float(line[1]) table.append(line) PrintList(table) file.close() And now I'd like to sort the list in increasing order of price

Efficiency of Bubble vs. Selection Sort

情到浓时终转凉″ 提交于 2019-12-02 01:20:27
I understand that the big O values for Bubble Sort and Selection Sort are the same, (n)^2, but when I try to run both with an array of size 1000, the Bubble Sort takes 962037 swaps to sort the array, while Selection Sort only takes 988 swaps to sort the array. Why are these different? Because the complexity refers to the number of comparisons, not the number of swaps. Both need O(n^2) comparisons, but selection sort needs only n-1 swaps in the worstcase (O(n)), whereas bubblesort might need up to n*(n-1)/2 swaps (O (n^2)). And even if the complexity would refer to the number of swaps - as the

Insertion Sort vs. Selection Sort

痞子三分冷 提交于 2019-11-29 18:35:59
I am trying to understand the differences between Insertion Sort and Selection Sort. They both seem to have two components: an unsorted list and a sorted list. They both seem to take one element from the unsorted list and put it into the sorted list at the proper place. I have seen some sites/books saying that selection sort does this by swapping one at a time while insertion sort simply finds the right spot and inserts it. However, I have seen other articles say something, saying that insertion sort also swaps. Consequently, I am confused. Is there any canonical source? Selection Sort: Given

Insertion Sort vs. Selection Sort

只愿长相守 提交于 2019-11-28 13:13:25
问题 I am trying to understand the differences between Insertion Sort and Selection Sort. They both seem to have two components: an unsorted list and a sorted list. They both seem to take one element from the unsorted list and put it into the sorted list at the proper place. I have seen some sites/books saying that selection sort does this by swapping one at a time while insertion sort simply finds the right spot and inserts it. However, I have seen other articles say something, saying that