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 implement insertion sorting in python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how to achieve python insertion sorting, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Insert sort:

Public static int [] insertionSort (int [] array) {if (array.length = = 0) return array; int current; for (int I = 0 preindex I = 0 & array [preindex] > current) {array [preindex+1] = array [preindex]; preindex--;} array [preindex+1] = current;} return array;}

First of all, I will give you a cartoon picture, so that you can easily understand:

In fact, you can feel from the figure that insertion sorting is a relatively simple sort, without too many complex steps. The basic principle of sorting is also very simple. For elements that are not sorted, scan from back to front in the sorted elements to find the appropriate position to insert.

In fact, in the same way that people organize bridge, one card at a time, each card is inserted into the appropriate place in other cards that are already in order.

Specific steps:

Step 1: starting with the first element, the element can be considered sorted; step 2: take out the next element and scan from back to forward in the sorted element sequence; step 3: if the element (sorted) is larger than the new element, move the element to the next location; step 4: repeat step 3 until the sorted element is found to be less than or equal to the new element

Step 5: after inserting the new element into this location; step 6: repeat step 2-5. The above is all the content of the article "how to insert sorting in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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

Internet Technology

Wechat

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

12
Report