Get the App
SLTechnology News&Howtos  ›  Servers  › 

Good programmer Python Learning Route sharing to implement merge sorting algorithm

Shulou Source: shulou.com Published: 2022-06-02 19:29:02 09月22日 Update

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]

Tags: Sorting problems recursion arrays parts algorithms elements ideas order similarity two middle code entry typical animation at the same time pictures articles conditions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Shulou Tech Info Linux macOS