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

Good programmer Python Learning Route sharing to implement merge sorting algorithm

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Implementation of merge sorting algorithm by Python

The merger sequence was first proposed by John von Neumann in 1945. This algorithm is a very typical application of divide-and-conquer (Divide and Conquer), and each layer of divide-and-conquer recursion can be carried out simultaneously.

The basic idea of divide-and-conquer law

The original problem is decomposed into several sub-problems with smaller scale but similar structure to the original problem. Recursively solve these sub-problems, and then combine the solutions of these sub-problems into the solution of the original problem.

The basic idea of merging and sorting

To sort an array, we first divide the array into front and back parts from the middle, then sort the front and back parts separately, and then merge the two parts together so that the whole array is in order.

Animation

(please insert the picture of ms.webp when you publish the article)

Code implementation

```python

Def merge (left, right):

'merge and sort'

I = 0

J = 0

Result = []

Length_left = len (left)

Length_right = len (right)

While I < length_left and j < length_right:

# compare the elements of the two lists one by one

# add the small ones to the new list, and leave the big ones to continue the comparison

If left [i]

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: 275

*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

Servers

Wechat

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

12
Report