In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what are the sorting algorithms in python, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Bubble sorting (Bubble Sort) is a simple sorting algorithm in the field of computer science. It repeatedly visits the element columns to be sorted, compares two adjacent elements in turn, and swaps them if the order (such as big to small, initials Z to A) is wrong. The work of visiting an element is repeated until there are no adjacent elements to swap, that is, the element column has been sorted.
Pseudo code
Function bubble sort enter an array name of array whose length is length I from 1 to (length-1) j from 0 to (length-1-I) if array [j] > array [j + 1] swaps the values of array [j] and array [j + 1] if the end of j loop ends I loop ends function ends
Def bubble_sorted (iterable): new_list = list (iterable) list_len = len (new_list) for i in range (list_len): for j in range (list_len-I-1): if new_ list [j] > new_ list [j + 1]: new_list [j], new_ list [j + 1] = new_ list [j + 1], new_ list [j] return new_list
Selective sorting (Selection sort) is a simple and intuitive sorting algorithm. How it works is as follows.
First find the smallest (large) element in the unsorted sequence and store it at the beginning of the sorted sequence, then continue to find the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on until all the elements are sorted.
Def selection_sort (alist): n = len (alist) for i in range (nmer1):
Min_index = i for j in range (iTun1jinn): if alist [j]
< alist[min_index]: min_index = j if min_index !=i: alist[i] ,alist[min_index] = alist[min_index] ,alist[i] return alist alist= [463,25,434,768,332,6657,34343,67]result=selection_sort(alist=alist) print(result) 选择排序的示例动画。红色表示当前最小值,黄色表示已排序序列,蓝色表示当前位置。 插入排序(英语:Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。 def insert_sort(alist): for i in range(1,len(alist)): for j in range(i,0,-1): if alist[j]= 2: # 递归入口及出口 mid = data[len(data)//2] # 选取基准值,也可以选取第一个或最后一个元素 left, right = [], [] # 定义基准值左右两侧的列表 data.remove(mid) # 从原始数组中移除基准值 for num in data: if num >= mid: right.append (num) else: left.append (num) return quick_sort (left) + [mid] + quick_sort (right) else: return data # example: array = [2, 3, 5, 5, 7, 5, 4, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 17 print 12] print (quick_sort (array)) # output as [1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 9, 9, 10, 12, 15, 15, 17] these are the sorting algorithms in python Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.