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 common sorting algorithms with python3

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today Xiaobian to share with you how to achieve common sorting algorithm python3 related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some harvest, let's learn about it together.

bubble sort

Bubble sort is a simple sort algorithm. It repeatedly visits the sequence to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The work of visiting a sequence is repeated until there is no more exchange, that is, the sequence has been sorted. The algorithm gets its name from the fact that smaller elements slowly "float" to the top of the sequence by swapping.

def mao(lst): for i in range(len(lst)): #Because at the end of each round, there must always be a big number behind #And the numbers are already lined up #That is, after round i, there are i numbers lined up #So the position from len-1 -i to len-1 is already arranged #Just compare 0 to len -1 -i # flag is used to mark whether the data is arranged at the beginning #Continue sorting only if flag status changes (first loop can be determined), otherwise return flag = False for j in range(len(lst) - i - 1): if lst[j] > lst[j + 1]: lst[j], lst[j + 1] = lst[j + 1], lst[j] flag = True #Unsorted data, change flag if not flag: return lst return lstprint(mao([1, 5, 55, 4, 5, 1, 3, 4, 5, 8, 62, 7])) Select sort

Selection sorting is a simple and intuitive sorting algorithm. How it works: first find the smallest (largest) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (largest) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are sorted.

#Selection sort is to find the smallest element in a list and put it first.# Bubble sorting is similar to #where the positions from 0 to i are arranged, just need to arrange i+1 to len(lst)-1 def select_sort(lst): for i in range(len(lst)): min_index = i #Index used to record the smallest element for j in range(i + 1, len(lst)): if lst[j] < lst[min_index]: min_index = j #At this point, it has been determined that min_index is the index of the minimum value in the interval i+1 to len(lst) - 1 lst[i], lst[min_index] = lst[min_index], lst[i] return lstdef select_sort2(lst): #The second way to choose sorting #Same principle as the first #However, there is no need to refer to the intermediate variable min_index #Just find the elements i+1 through len(lst) after index i for i in range(len(lst)): for j in range(len(lst) - i): # lst[i + j] is a number from i to len(lst)-1 Because J

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

Development

Wechat

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

12
Report