python - Comb Sorting Program -


i trying write program in python should sort list within list.

examples -

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], [2, 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], [2, 1], [2, 2], [1, 2], [3, 1]] list after sorting: [[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 3]] 

my code:

import math  def combsort(list1):     gap = len(list1)     shrink = 1.3     sorted = false      while sorted == false:         gap = gap/shrink         if gap > 1:             sorted = false         else:             gap = 1             sorted = true          = 0         while + gap < gap:             distance1 = math.sqrt(list1[i[0]]**2 + list1[i[1]]**2)             distance2 = math.sqrt(list1[i+gap[0]]**2 + list1[i+gap[1]]**2)             if distance1 > distance2:                 temporary = list1[i]                 list1[i] = list1[i + gap]                 temporary = list1[i + gap]                 sorted = false             = + 1  list1 = [[2, 1], [2, 2], [3, 3], [3, 1], [1, 1], [1, 2]]  combsort(list1) print(list1) 

my code doesn't work , prints out exact same list. help?

this given follow:

comb sort variation of bubble sort performs more efficient sorting. accomplishes moving low values near end of list further toward front of list bubble sort during iterations.

implement function called combsort following:

  1. takes input 2d list contains information representing x/y points in 2d space. each item in list list 2 items x , y coordinate. example, list [ [0, 1],[2, 1], [3, 3], [1, 1], … ]
  2. list item performs in-place sort (i.e., not create new list, modifies original) using comb sort algorithm sorts 2d list such points lower 2 euclidean distance origin (0, 0) appear earlier in list. in case, comparing distances instead of directly comparing list values – may useful implement , use distance calculation function. note – euclidean distance of point (x, y) origin (0, 0) can calculated following equation: distance(x,y) = �! + �!
  3. does not return value. input list sorted in place, modified directly , these modifications reflected outside function, return value not needed.

the while loop never gets executed because condition cannot true:

while + gap < gap: 

hint: use more print statements or debugger check values of things while program running.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -