详解排序算法(Python实现)

简介: 详解排序算法(Python实现)

Python的内置排序算法

与许多其他高级编程语言一样,Python语言提供了使用sorted()函数对数据进行开箱即用的功能。示例:

>>> li = [9, 5, 3, 6, 7]
>>> sorted(li)
[3, 5, 6, 7, 9]

冒泡排序

冒泡排序是最直接的排序算法之一。它的名称来自算法的工作方式:每经过一次便利,列表中最大的元素就会“冒泡”至正确位置。

冒泡排序包括:遍历一个列表,一次比较元素,以及交换不规则的相邻项。

Python实现冒泡排序

def bublle_sort(array):
  n = len(array)
  for i in range(n):
    # 创建一个标志位
    already_sorted = True
    for j in range(n - i - 1):
      if array[j] > array[j+1]:
        array[j], array[j+1] = array[j+1], array[j]
        already_sorted = False
    if already_sorted:
            break
     return array

时间复杂度:O( n 2 n^2 n2).

插入排序

像冒泡排序一样,插入排序算法也易于实现和理解。但是与冒泡排序不同,它通过将每个项目与列表的其余部分进行比较并将其插入正确的位置,来一次构建一个排序的列表元素。此“插入”过程为算法命名。

解释插入排序的一个很好的类比是您对一副纸牌进行排序的方式。想象一下,您手里拿着一组卡片,并且想要按顺序排列它们。首先,将一张卡片与其余卡片进行逐步比较,直到找到正确的位置为止。届时,您需要将卡插入正确的位置,然后重新开始制作新卡,重复进行直到您手上的所有卡都被排序为止。

Python实现插入排序

def insertion_sort(array):
  for i in range(1, len(array)):
    key_item = array[i]
    j = i - 1
    while j >= 0 and array[j] > key_item:
      array[j + 1] = array[j]
      j -= 1
    array[j + 1] = key_item
  return arry

时间复杂度:O( n 2 n^2 n2).

归并排序

归并排序是一种非常有效的排序算法。它基于分治法,这是一种用于解决复杂问题的强大算法技术。

为了正确理解分而治之,您应该首先了解递归的概念。递归涉及将问题分解为较小的子问题,直到它们足够小以至于无法解决。在编程中,递归通常由调用自身的函数表示。

分而治之算法通常遵循相同的结构:

  1. 原始输入分为几个部分,每个部分代表一个子问题,该子问题与原始输入相似,但更为简单。
  2. 每个子问题都递归解决。
  3. 所有子问题的解决方案都组合成一个整体解决方案。

在归并排序的情况下,分而治之的方法将输入值的集合划分为两个大小相等的部分,对每个一半进行递归排序,最后将这两个排序的部分合并为一个排序列表。

Python实现归并

def merge(left, right):
  if len(left) == 0:
    return right
  if len(right) == 0:
    return left
  result = []
  index_left = index_right = 0
  while len(result) < len(left) + len(right):
    if left[index_left] <= right[index_right]:
      result.append(left[index_left])
      index_right += 1
    else:
      result.append(right[index_right])
      index_left += 1
     if index_right == len(right):
            result += left[index_left:]
            break
        if index_left == len(left):
            result += right[index_right:]
            break
    return result
def merge_sort(array):
    if len(array) < 2:
        return array
    midpoint = len(array) // 2
    return merge(
        left=merge_sort(array[:midpoint]),
        right=merge_sort(array[midpoint:]))

时间复杂度: O( n l o g n n logn nlogn)

快速排序

就像合并排序一样,快速排序算法采用分而治之的原理将输入数组分为两个列表,第一个包含小项目,第二个包含大项目。然后,该算法将对两个列表进行递归排序,直到对结果列表进行完全排序为止。

划分输入列表称为对列表进行分区。Quicksort首先选择一个枢轴元素,然后将列表围绕该枢轴进行分区,将每个较小的元素放入一个低数组,将每个较大的元素放入一个高数组。

将每个元素从低位列表放置到数据透视表的左侧,将每个元素从高位列表放置在数据透视表的右侧,将其精确定位在最终排序列表中需要的位置。这意味着函数现在可以递归地将相同的过程递归地从低到高,直到对整个列表进行排序。

Python实现快速排序

rom random import randint
def quicksort(array):
    # If the input array contains fewer than two elements,
    # then return it as the result of the function
    if len(array) < 2:
        return array
    low, same, high = [], [], []
    # Select your `pivot` element randomly
    pivot = array[randint(0, len(array) - 1)]
    for item in array:
        # Elements that are smaller than the `pivot` go to
        # the `low` list. Elements that are larger than
        # `pivot` go to the `high` list. Elements that are
        # equal to `pivot` go to the `same` list.
        if item < pivot:
            low.append(item)
        elif item == pivot:
            same.append(item)
        elif item > pivot:
            high.append(item)
    # The final result combines the sorted `low` list
    # with the `same` list and the sorted `high` list
    return quicksort(low) + same + quicksort(high)

时间复杂度: O( n l o g n n logn nlogn)

Timsort algorithm

Timsort算法被认为是一种混合排序算法,因为它采用了插入排序和合并排序的两全其美组合。Timsort与Python社区近在咫尺,因为它是由Tim Peters在2002年创建的,被用作Python语言的标准排序算法。

Timsort的主要特征是它利用了大多数现实数据集中存在的已排序元素。这些称为自然运行。然后,该算法会遍历列表,将元素收集到运行中,然后将它们合并到一个排序的列表中。

Python 实现Timsort

在本部分中,您将创建一个准系统的Python实现,该实现说明Timsort算法的所有部分。如果您有兴趣,也可以查看Timsort的 原始C实现

def insertion_sort(array, left=0, right=None):
    if right is None:
        right = len(array) - 1
    # Loop from the element indicated by
    # `left` until the element indicated by `right`
    for i in range(left + 1, right + 1):
        # This is the element we want to position in its
        # correct place
        key_item = array[i]
        # Initialize the variable that will be used to
        # find the correct position of the element referenced
        # by `key_item`
        j = i - 1
        # Run through the list of items (the left
        # portion of the array) and find the correct position
        # of the element referenced by `key_item`. Do this only
        # if the `key_item` is smaller than its adjacent values.
        while j >= left and array[j] > key_item:
            # Shift the value one position to the left
            # and reposition `j` to point to the next element
            # (from right to left)
            array[j + 1] = array[j]
            j -= 1
        # When you finish shifting the elements, position
        # the `key_item` in its correct location
        array[j + 1] = key_item
    return array

此修改后的实现添加了左右两个参数,这些参数指示应对数组的哪一部分进行排序。这使Timsort算法可以对数组的一部分进行排序。修改功能而不是创建新功能意味着可以将其同时用于插入排序和Timsort。

def timsort(array):
    min_run = 32
    n = len(array)
    # Start by slicing and sorting small portions of the
    # input array. The size of these slices is defined by
    # your `min_run` size.
    for i in range(0, n, min_run):
        insertion_sort(array, i, min((i + min_run - 1), n - 1))
    # Now you can start merging the sorted slices.
    # Start from `min_run`, doubling the size on
    # each iteration until you surpass the length of
    # the array.
    size = min_run
    while size < n:
        # Determine the arrays that will
        # be merged together
        for start in range(0, n, size * 2):
            # Compute the `midpoint` (where the first array ends
            # and the second starts) and the `endpoint` (where
            # the second array ends)
            midpoint = start + size - 1
            end = min((start + size * 2 - 1), (n-1))
            # Merge the two subarrays.
            # The `left` array should go from `start` to
            # `midpoint + 1`, while the `right` array should
            # go from `midpoint + 1` to `end + 1`.
            merged_array = merge(
                left=array[start:midpoint + 1],
                right=array[midpoint + 1:end + 1])
            # Finally, put the merged array back into
            # your array
            array[start:start + len(merged_array)] = merged_array
        # Each iteration should double the size of your arrays
        size *= 2
    return array

此修改后的实现添加了左右两个参数,这些参数指示应对数组的哪一部分进行排序。这使Timsort算法可以对数组的一部分进行排序。修改功能而不是创建新功能意味着可以将其同时用于插入排序和Timsort。

相关文章
|
2月前
|
算法 搜索推荐 JavaScript
基于python智能推荐算法的全屋定制系统
本研究聚焦基于智能推荐算法的全屋定制平台网站设计,旨在解决消费者在个性化定制中面临的选择难题。通过整合Django、Vue、Python与MySQL等技术,构建集家装设计、材料推荐、家具搭配于一体的一站式智能服务平台,提升用户体验与行业数字化水平。
|
2月前
|
Java 数据处理 索引
(Pandas)Python做数据处理必选框架之一!(二):附带案例分析;刨析DataFrame结构和其属性;学会访问具体元素;判断元素是否存在;元素求和、求标准值、方差、去重、删除、排序...
DataFrame结构 每一列都属于Series类型,不同列之间数据类型可以不一样,但同一列的值类型必须一致。 DataFrame拥有一个总的 idx记录列,该列记录了每一行的索引 在DataFrame中,若列之间的元素个数不匹配,且使用Series填充时,在DataFrame里空值会显示为NaN;当列之间元素个数不匹配,并且不使用Series填充,会报错。在指定了index 属性显示情况下,会按照index的位置进行排序,默认是 [0,1,2,3,...] 从0索引开始正序排序行。
257 0
|
3月前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
193 26
|
3月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于D*算法的机器人路径规划(Python代码实现)
189 0
|
3月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于改进型A*算法的机器人路径规划(Python代码实现)
231 0
|
3月前
|
机器学习/深度学习 编解码 算法
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
324 4
|
3月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
【机器人路径规划】基于A*算法的机器人路径规划研究(Python代码实现)
471 4
|
3月前
|
机器学习/深度学习 算法 机器人
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
【机器人路径规划】基于深度优先搜索(Depth-First-Search,DFS)算法的机器人路径规划(Python代码实现)
252 3
|
3月前
|
算法 机器人 定位技术
【机器人路径规划】基于流场寻路算法(Flow Field Pathfinding)的机器人路径规划(Python代码实现)
【机器人路径规划】基于流场寻路算法(Flow Field Pathfinding)的机器人路径规划(Python代码实现)
165 4
机器学习/深度学习 算法 自动驾驶
551 0

推荐镜像

更多