python - fail to return sorted shell list -


i have constructed shell sort algorithm pseudocode , c code in python script..

(i working through algorithms exercise)

i cannot seem sorted list return

when print(inner) part of loop, indeed print them out in order 1 1 , ends..

2 3 4 5 6 7 8 9

i have tried return inner function

the response getis [[9]]

which last iteration.. know iterates, sorts correctly..

however cannot return sorted list

[1, 2, 3, 4, 5, 6, 7, 8, 9]

unsort_list = [4, 6, 3, 2, 1, 9, 7, 8, 5]  def shell(a):     """      step 1 − initialize value of h     step 2 − divide list smaller sub-list of equal interval h     step 3 − sort these sub-lists using insertion sort     step 3 − repeat until complete list sorted      """      interval_h = 1     = 0     l = len(a)     elements =     inner = []     outer = []     value_ins = []      print(l)      while interval_h <= l / 3:         interval_h = interval_h * 3 + 1      while interval_h > 0:         outer = interval_h         in elements:             while outer < i:                 outer += 1                 value_ins = [outer]                 inner = outer                  while inner > interval_h - 1 , [inner - interval_h] >= value_ins:                     inner = [inner - interval_h]                     inner -= interval_h              inner = value_ins          interval_h = (interval_h - 1)          += 1     return inner  sorted_list = [shell(unsort_list)] print(sorted_list) 

after stripping , rebuilding after taking on board above advice have come solution using enumerate.

thanks advice

def maf_shell(a):     """      step 1 − initialize value of h     step 2 − divide list smaller sub-list of equal interval (h)     step 3 − sort these sub-lists using insertion sort     step 3 − repeat until complete list sorted      """     interval = len(a) // 2      while interval:         i, j in enumerate(a):             while >= interval , a[i - interval] > j:                 a[i] = a[i - interval]                 -= interval             a[i] = j         interval = 1 if interval == 2 else int(interval * 5.0 / 11)                    unsort_list = [4, 6, 3, 2, 1, 9, 7, 8, 5]   maf_shell(unsort_list)  print(unsort_list) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -