Home > Backend Development > Python Tutorial > What are the Python list sorting methods?

What are the Python list sorting methods?

coldplay.xixi
Release: 2020-09-11 13:34:57
Original
23106 people have browsed it

Python list sorting: 1. Bubble sorting is a simple sorting algorithm. It repeatedly traverses the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order; 2. Insertion sort, by constructing an ordered sequence, for unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it.

What are the Python list sorting methods?

Related learning recommendations: python tutorial

1. Bubble sorting

Bubble Sort is a simple sorting algorithm. It repeatedly iterates through the array to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The work of traversing the array is repeated until no more exchanges are needed, which means that the array has been sorted. The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the array through swapping.

What are the Python list sorting methods?

def bubble_sort(list):
    n = len(list)
    for i in range(n - 1):
        for j in range( 0,n - 1 - i):
            if list[j] > list[j + 1]:
                list[j], list[j + 1] = list[j + 1], list[j]
        # if list[i] > list[i + 1]:
        #     list[i], list[i + 1] = list[i + 1], list[i]
    print(list)
list=[2,4,6,8,1,3,5,7,9]
bubble_sort(list)
#结果:[1,2,3,4,5,6,7,8,9]
Copy after login

2. Insertion Sort

Insertion Sort is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, it scans from back to front in the sorted sequence, finds the corresponding position and inserts it. In the implementation of insertion sort, during the scanning process from back to front, the sorted elements need to be gradually moved backward to provide insertion space for the latest elements.

What are the Python list sorting methods?

def insertion_sort(list):
  n = len(list)
  for i in range(1, n):
    for j in range(i, 0, -1):
      if list[j] < list[j - 1]:
        list[j], list[j - 1] = list[j - 1], list[j]
      else:
        break
  print(list)
insertion_sort([3,0,2,5,8,5,9,41,0,1,6])
#结果:[0, 0, 1, 2, 3, 5, 5, 6, 8, 9, 41]
Copy after login

3. Selection Sort

Selection Sort is a simple and intuitive sorting algorithm. Its working principle is as follows: first find the smallest (large) element in the unsorted sequence, store it at the starting position of the sorted sequence, and then continue to find the smallest (large) element from the remaining unsorted elements. placed at the end of the sorted sequence. And so on until all elements are sorted.

def selection_sort(list):
    n = len(list)
    for i in range(0, n -1):
        min_index = i
        for j in range(i + 1, n):
            if list[min_index] > list[j]:
                min_index = j
        if i != min_index:
            list[min_index], list[i] = list[i], list[min_index]
    print(list)
selection_sort([5,9,6,42,9,4,66,2,3,0,1])
#结果:0, 1, 2, 3, 4, 5, 6, 9, 9, 42, 66]
Copy after login

Summary:

import random
# 随机生成1-1000之间无序序列整数数据
def generator():
    random_data = []
    for i in range( 0, 10 ):
        random_data.append( random.randint( 1, 1000 ) )
    return random_data
# 冒泡排序
def bubble_sort(list):
    # 序列长度
    n = len( list )
    for i in range( 0, n ):
        for j in range( i , n ):
            if list[i] > list[j]:
                list[i], list[j] = list[j], list[i]
    return list
 # 选择排序
def selection_sort(list):
    n = len(list)
    for i in range(0, n -1):
        min_index = i
        for j in range(i + 1, n):
            if list[min_index] > list[j]:
                min_index = j
        if i != min_index:
            list[min_index], list[i] = list[i], list[min_index]
    return list
#插入排序
def insertion_sort(list):
  n = len(list)
  for i in range(1, n):
    for j in range(i, 0, -1):
      if list[j] < list[j - 1]:
        list[j], list[j - 1] = list[j - 1], list[j]
      else:
        break
  return list
if __name__ == "__main__":
    # 生成随机无序数据
    list = generator()
    # 打印无序数据
    print( &#39;随机生成的无序数据:&#39;,list )
    # 冒泡排序
    sorted_data = bubble_sort( list )
    #插入排序
    insertion_data=insertion_sort(list)
    #选择排序
    selection_data=selection_sort( list )
    # 打印排序结果
    print( &#39;冒泡排序:&#39;,sorted_data )
    print( &#39;插入排序:&#39;, insertion_data )
    print( &#39;选择排序:&#39;, selection_data )
&#39;&#39;&#39;
结果:
随机生成的无序数据: [300, 517, 591, 209, 204, 789, 417, 739, 803, 393]
冒泡排序: [204, 209, 300, 393, 417, 517, 591, 739, 789, 803]
插入排序: [204, 209, 300, 393, 417, 517, 591, 739, 789, 803]
选择排序: [204, 209, 300, 393, 417, 517, 591, 739, 789, 803]
&#39;&#39;&#39;
Copy after login

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of What are the Python list sorting methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template