Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python Hill sort

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use python hill sorting, the introduction in the article is very detailed, has a certain reference value, interested friends must read it!

1. Sort according to adjacent elements. If the direct insertion sort is step 1, then Hill sort is to insert sort first by step K.

2. Then sort step m on the basis of step K sorting, K is greater than m, and finally sort step 1.

examples

def shell_sort(data_list): ''' Idea: Divide and conquer strategy use a for loop ''' length = len(data_list) space = length//2 while space > 0: for i in range(space,length ): #By default, the first element is a sorted interval, so subscripts start at 1 tmp = data_list[i] #Data to be inserted index = i for j in range(i-space,-1, -space): #Find insertion position from sorted interval if tmp

< data_list[j]: data_list[j+space] = data_list[j] #元素向后移动,腾出插入位置 index = j #最后的j即为插入的位置 else: break data_list[index] = tmp #插入操作 print(data_list) space = space // 2 return data_list def shell_sort2(data_list): ''' 思想:分治策略 使用 while 循环 ''' length = len(data_list) space = length//2 while space >

0: i = space while i

< length: #默认第一个位置的元素是已排序区间,因此下标从 1 开始 tmp = data_list[i] #待插入的数据 j = i while j >

= space and data_list[j - space] > tmp: #Find insertion position from sorted interval data_list[j] = data_list[j-space] #Move elements backward to make room for insertion j -= space data_list[j] = tmp #insert print(data_list) i +=1 space = space // 2 return data_list The above is "Python Hill sorting how to use" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report