斯大林排序

斯大林排序– sort through,有数据丢失的排序算法之一。
该算法非常高效高效,时间复杂度为O(n)。

它的工作原理如下:

  1. 循环数组,将当前元素与下一个元素进行比较
  2. 如果下一个元素小于当前元素,则将其删除
  3. 因此,我们在 O(n) 时间内得到了一个排序数组

算法输出示例:

Gulag: [1, 3, 2, 4, 6, 42, 4, 8, 5, 0, 35, 10]
Element 2 sent to Gulag
Element 4 sent to Gulag
Element 8 sent to Gulag
Element 5 sent to Gulag
Element 0 sent to Gulag
Element 35 sent to Gulag
Element 10 sent to Gulag
Numbers: [1, 3, 4, 6, 42]
Gulag: [2, 4, 8, 5, 0, 35, 10]

Python 3 代码:

gulag = []

print(f"Numbers: {numbers}")
print(f"Gulag: {numbers}")

i = 0
maximal = numbers[0]

while i < len(numbers):
    element = numbers[i]
    if maximal > element:
        print(f"Element {element} sent to Gulag")
        gulag.append(element)
        del numbers[i]
    else:
        maximal = element        
        i += 1

print(f"Numbers: {numbers}")
print(f"Gulag: {gulag}")

缺点包括数据丢失,但如果我们朝着乌托邦式的、理想的、O(n) 的排序列表发展,那还能怎样呢?

链接

https://gitlab.com/demensdeum /algorithms/-/tree/master/sortAlgorithms/stalinSort

来源

https://github.com/gustavo-depaula/stalin-sort
https://www.youtube.com/shorts/juRL-Xn-E00
https://www.youtube.com/watch?v=L78i2YcyYfk

Leave a Comment

Your email address will not be published. Required fields are marked *