Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of the running process of python insertion sort

Shulou Source: shulou.com Published: 2022-06-03 17:21:30 09月11日 Update

This article mainly introduces the example analysis of the running process of python insertion sorting, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

In order to ensure that the data is still orderly after insertion, it is necessary to determine the location of the inserted data.

1. Divide the data to be sorted into two intervals.

Ordered interval and disordered interval. The initial ordered interval contains only one element, the first element of the array, and the other is the unordered interval.

2. Select an element from the unordered interval in turn, and find a suitable insertion position in the ordered interval to ensure that the data in the sorted interval is always orderly.

3. Repeat this process.

Until the element of the unordered interval is empty, the algorithm ends.

Example

# encoding=utf-8def insert_sort (data_list):''No optimized version''count=0 # counts the number of cycles length = len (data_list) for i in range (1): # the element in the first position is the sorted interval by default So the subscript starts at 1 tmp = data_ list [I] # data to be inserted j = i while j > 0: # find the insertion position count + = 1 if tmp < data_ list [j-1]: data_ list [j] = data_ list [j-1] # element moves backward Vacate the insertion position else: break j-= 1 data_ list [j] = tmp # insert operation print (data_list) print (f "total number of loops is {count}") return data_list thank you for reading this article carefully. I hope the article "sample Analysis of the running process of python insertion sorting" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Tags: Interval sort element order location data article process example analysis run number loop fit subscript two value interest and at the same time instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology vpn Linux MariaDB