A Guide to Sorting Algorithms in Python
This article introduces four fundamental sorting algorithms—bubble sort, insertion sort, merge sort, and quick sort—explaining their concepts, step‑by‑step operations, and providing complete Python implementations to help readers understand and apply these techniques effectively.
Sorting is a core algorithmic technique that enables efficient data organization; this guide focuses on four classic sorting algorithms and demonstrates how to implement each in Python.
Bubble Sort repeatedly compares adjacent elements and swaps them if they are out of order, iterating through the list until it is sorted.
# Python中的冒泡排序
def bubbleSort(array):
# 外循环访问数组的每个元素
for i in range(len(array)):
# 内循环将数组元素与外循环迭代元素进行比较
for j in range(0, len(array) - i - 1):
# 比较两个相邻元素
if array[j] > array[j + 1]:
# 如果元素不是预期顺序则交换元素
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
data = [5, 4, 3, 2, 1]
bubbleSort(data)
print('Sorted Array')
print(data)
#output: [1, 2, 3, 4, 5]Insertion Sort builds a sorted portion of the array by repeatedly taking the next unsorted element and inserting it into its correct position within the sorted part.
# Python中的排序算法
def insertionSort(array):
for step in range(1, len(array)):
key = array[step]
j = step - 1
# 将键与其左侧的每个元素进行比较,直到找到小于它的元素
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j = j - 1
# 将键放在比它小的元素之后。
array[j + 1] = key
data = [11, 4, 3, 2, 12]
insertionSort(data)
print("sorted array")
print(data)
#output: [2, 3, 4, 11, 12]Merge Sort is a divide‑and‑conquer algorithm that recursively splits the array into halves, sorts each half, and then merges the sorted halves back together.
# Python的归并排序
def mergeSort(array):
if len(array) > 1:
# r 是将数组分为两半后的分割点
r = len(array)//2
L = array[:r]
M = array[r:]
# 通过递归方法对两半进行排序
mergeSort(L)
mergeSort(M)
i = j = k = 0
# 合并两个已排序的子数组
while i < len(L) and j < len(M):
if L[i] < M[j]:
array[k] = L[i]
i += 1
else:
array[k] = M[j]
j += 1
k += 1
# 复制剩余元素
while i < len(L):
array[k] = L[i]
i += 1
k += 1
while j < len(M):
array[k] = M[j]
j += 1
k += 1
array = [8, 6, 14, 12, 10, 3]
mergeSort(array)
print("Sorted array: ")
print(array)
#output: [3, 6, 8, 10, 12, 14]Quick Sort also uses the divide‑and‑conquer strategy; it selects a pivot element, partitions the array around the pivot, and then recursively sorts the sub‑arrays.
# Python中的快速排序
def partition(array, lowest, highest):
# 这里我们选择最右的元素作为枢轴
pivot = array[highest]
i = lowest - 1
for j in range(lowest, highest):
if array[j] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[highest]) = (array[highest], array[i + 1])
return i + 1
def quickSort(array, lowest, highest):
if lowest < highest:
pi = partition(array, lowest, highest)
quickSort(array, lowest, pi - 1)
quickSort(array, pi + 1, highest)
array = [9, 8, 3, 2, 1, 10, 7, 6, 19]
size = len(array)
quickSort(array, 0, size - 1)
print('Sorted Array is below')
print(array)
#output [1, 2, 3, 6, 7, 8, 9, 10, 19]The article concludes with a brief thank‑you note and a link to the original source.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.