Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement selective sorting in Python

Shulou Source: shulou.com Published: 2022-06-02 08:16:12 09月23日 Update

This article introduces how to achieve selection sorting in Python, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Select sort (Selection Sort)

Selection Sort is an improvement based on bubble sorting, which retains multiple comparisons in bubble sorting, and each trip still moves the largest item to the end. The difference is that the bubble sort compares each adjacent element whether it needs to be swapped or not, but the selective sort is the index that records the maximum value, which is swapped with the last element after traversing the list of the current trip.

Select the number of comparisons for sorting time complexity: O (n ^ 2)

Select the number of exchanges for sorting time complexity: O (n)

Bubble sort: Python data structure and algorithm-Bubble sort

Select sort key: record the maximum item location

The idea of the algorithm:

1. The first selection sort: nMel 1 comparison, record the location of the largest item, exchange with the last element

two。 The second selection sort: 1 element of NMUE, 2 comparisons of NMUE, record the position of the largest item, and exchange with the last element of the current trip.

3. The third trip is sorted by selecting 2 elements, comparing 3 times, recording the position of the largest item, and exchanging with the last element of the current trip.

……

NMUE 1. N-1 th selection sort: direct comparison of 2 elements

On how to achieve selection sorting in Python to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Sort select element maximum location complexity content complexity time more maximum number of times algorithm help different good got different key interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Tech Info Microsoft Docker