为Python中的数值数据集编写排序算法?
排序是指以特定格式排列数据。它使数据更具可读性,并且可以将数据搜索优化到很高的水平。有五种不同的排序算法。
气泡排序
合并排序
插入排序
贝壳类
选择排序
示例
def bubblesort(list): # Swap the elements to arrange in order for iter_num in range(len(list)-1,0,-1): for idx in range(iter_num): if list[idx]>list[idx+1]: temp = list[idx] list[idx] = list[idx+1] list[idx+1] = temp list = [22,43,3,21,12,31,1,2,19,10] bubblesort(list) print(list)
这是一种基于比较的算法,其中比较每对相邻元素,如果元素顺序不正确,则将其交换。
输出结果
[1, 2, 3, 10, 12, 19, 21, 22, 31, 43]
热门推荐
7 万寿祝福语简短
10 理科元旦祝福语大全简短
11 高考送考祝福语简短
12 夸赞老师祝福语搞笑简短
13 近代生日祝福语简短精辟
14 婆婆生日祝福语短语简短
15 给宝贝开学祝福语简短
16 写给情侣祝福语 浪漫简短
17 公司开年仪式祝福语简短
18 妈妈给宝贝祝福语简短