can i get help with this python code? Comb sort is a variation of bubble sort th
ID: 3802617 • Letter: C
Question
can i get help with this python code?
Comb sort is a variation of bubble sort that generally performs more efficient sorting. It accomplishes this by moving low values near the end of the list further toward the front of the list than bubble sort would during early iterations. Pseudocode for the comb sort algorithm is included at the end of this document. Create a Python file called comb.py and implement a function called combsort that does the following: 1. Takes as input a 2D list that contains information representing x/y points in 2D space. Each item in the list will be a list with 2 items - an x and a y coordinate. For example, the list could be [[0, 1], [2, 1], [3, 3], [1, 1], ...] 2. Performs an in-place sort (i.e., does not create a new list, but modifies the original) using the comb sort algorithm that sorts the 2D list such that points with lower 1 COMP 1005/1405-W17-A5 Due Friday, April 7 at 11:55 PM Euclidean distance to the origin (0, 0) appear earlier in the list. In this case, you are comparing distances instead of directly comparing list values - it may be useful to implement and use a distance calculation function. Does not return a value. As the input list is sorted in place, it will be modified directly and these modifications will be reflected outside the function, so a return value is not needed. Example outputs of original lists and the same lists after the combsort function has been called: List before sorting: [[2, 1], [2, 2], [3, 3], [3, 1], [1, 1], [1, 2]] List after sorting: [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]] List before sorting: [[3, 3], [2, 2), [1, 2], 12, 1], [3, 1], [1, 1]] List after sorting: [[1, 1], [2, 1], [1, 2], [2, 2], [3, 1], [3, 3]] List before sorting: [[1, 1], [3, 3], 12, 1], 12, 2], [1, 2], [3, 1]] List after sorting: [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]]Explanation / Answer
Here, Itemgetter of operator module,sorting it this makes the program more efficient. Read more about itemgetter in pyrhon documentation.
Steps:
1) Make the lists of list
2)Use sorted method
# cook your dish her
from operator import itemgetter
Mylist = [[2,1],[3,3],[2,2],[3,1],[1,1],[1,2]]
print ("List before sorting {}".format(Mylist))
print ("List after sorting {}".format(sorted(Mylist,key=itemgetter(0,1))))
Output:
List before sorting [[2, 1], [3, 3], [2, 2], [3, 1], [1, 1], [1, 2]]
List after sorting [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.