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

What are the sorting algorithms of Python

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

Share

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

This article mainly introduces "what are the Python sorting algorithms". In the daily operation, I believe many people have doubts about the Python sorting algorithm. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the Python sorting algorithms?" Next, please follow the editor to study!

Random value

The test data this time is different from the last one, which is really too regular, so random values are used this time:

From random import randint

Data = [randint (6, 20000) for i in range (30000)]

Data.insert (500,5)

Data.insert (700,7)

Data.insert (900,9)

And also in the generated list of random values to insert 3 values at different locations, with irregular random values, and then you can start the test.

Bubble sort def bubble (data):

For i in range (len (data)-1): # sorting times

For s in range (len (data)-iMur1): # s is the subscript of the list

If data [s] > data [sound1]:

Data [s], data [s] = data [s], data [s]

Return data

Start_time = datetime.now ()

Res = bubble (data)

Print (datetime.now ()-start_time)

Print (len (res), res [: 5], res [700 res 705], res [10000 res 10005])

The output is as follows:

0:01:20.273247

30003 [5, 6, 7, 7, 7] [492, 492, 492, 493, 495] [6665, 6665, 6666, 6668, 6668]

80 seconds! The test results of bubble sorting show that the list sorting of random elements takes longer than that of regular elements.

Select sort (two-tier for) def selections (nums):

For i in range (len (nums)):

Min_index = min (nums) # minimum

For j in range (len (nums)-I):

If nums[min _ index]

< nums[j]: min_index = j nums[min_index], nums[len(nums) - i - 1] = nums[len(nums) - i - 1], nums[min_index] return nums start_time = datetime.now() res = selections(data) print(datetime.now() - start_time) print(len(res), res[:5], res[700:705], res[10000:10005]) 得到的结果为: 0:01:07.171114 30003 [6, 6, 7, 7, 8] [444, 445, 445, 446, 447] [6652, 6654, 6654, 6654, 6654] 本次耗时 67 秒,而之前使用规律的值排序时耗时约 47 秒。选择排序(两层 for)的测试结果同样证明了随机元素的列表排序比规律元素的列表排序费时更久。 选择排序(min max)start_time = datetime.now() res = [] for i in range(0, len(data)): aps = min(data) data.remove(aps) res.append(aps) print(datetime.now() - start_time) print(len(res), res[:5], res[700:705], res[10000:10005]) 运行后得到的输出结果为: 0:00:10.102158 30003 [5, 6, 6, 7, 7] [443, 443, 443, 444, 444] [6645, 6646, 6649, 6650, 6650] 这一次耗时 10 秒,甚至比之前规律元素排序耗费的 14 秒更省时间。 插入排序def direct_insert(nums): # 崔庆才丨静觅、韦世东丨奎因 邀请你关注微信公众号【进击的Coder】 for i in range(1, len(nums)): temp = nums[i] # temp变量指向尚未排好序元素(从第二个开始) j = i-1 # j指向前一个元素的下标 while j >

= 0 and temp < nums [j]:

# temp compared with the previous element, if the temp is smaller, the previous element moves backward, j subtracts itself, and continues to compare

Nums [juni1] = nums [j]

J = jmur1

Nums [jacks 1] = the final location of the element pointed to by temp # temp

Return nums

Start_time = datetime.now ()

Res = direct_insert (data)

Print (datetime.now ()-start_time)

Print (len (res), res [: 5], res [700 res 705], res [10000 res 10005])

The output after running is as follows:

0:00:57.681174

30003 [5, 6, 6, 7, 7] [455, 456, 459, 459, 460] [6647, 6649, 6649, 6649, 6649]

This time the speed of inserting sort is no longer outrageous, within the range of conjecture.

The fog cleared away

Compared to the last time you used the very regular [i for i in range (3000)], the list generated with randint must be very irregular:

Print (data [: 20])

Print the top 20 elements of the list, and the result is:

[13698, 19871, 8468, 8735, 3473, 510, 788, 5070, 14585, 13324, 11743, 4310, 16460, 7102, 1900, 16608, 12342, 9724, 1482, 19609]

The values of these elements have hundreds, thousands and thousands of bits, which proves that they are indeed irregular.

The results of many tests are almost the same. among the above sorted tests, the fastest sorting of about 30,000 data is selective sorting (min max), which keeps the sorting speed within 10 seconds.

At this point, the study of "what are the Python sorting algorithms" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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