基本排序算法的python实现
**基本排序算法的python实现
1、冒泡排序
算法步骤
比较相邻的元素。如果第一个比第二个大,就交换他们两个。对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。针对所有的元素重复以上的步骤,除了最后一个。持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
    def bubble_sort(collection):
    
        length = len(collection)
        for i in range(length - 1):
            swapped = False
            for j in range(length - 1 - i):
                if collection[j] > collection[j + 1]:
                swapped = True
                collection[j], collection[j + 1] = collection[j + 1], collection[j]
        if not swapped: break  # Stop iteration if the collection is sorted.
    return collection
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print( *bubble_sort(unsorted), sep=',')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
最好情况:O(n)
最坏情况:O(n*2)
优点:链表结构和数组结构都可以使用,具有稳定性
2、选择排序
2.1 算法步骤
1.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置
2 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。
3 重复第二步,直到所有元素均排序完毕
算法复杂度的关键就是每次找序列的最小元,后面的堆排序就是对选择排序的改进,利用最小堆查找最小元。
def selection_sort(collection):
    length = len(collection)
    for i in range(length - 1):
        least = i
        for k in range(i + 1, length):
            if collection[k] < collection[least]:
                least = k
        collection[least], collection[i] = (
            collection[i], collection[least]
        )
    return collection
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    #unsorted = [9,4,7,3,18,5,22]
    print(*selection_sort(unsorted)) 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
3、插入排序
3.1 算法步骤
将待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。
从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。)
最好情况:O(n)
最坏情况:O(n*2)
复杂度与输入规模和逆序对的个数有关
优点:具有稳定性,交换的次数较少,移位的次数较多
def insertion_sort(collection):
    length = len(collection)
    for loop_index in range(1, length):
        insertion_index = loop_index
        while insertion_index > 0 and collection[insertion_index - 1] > collection[insertion_index]:
            collection[insertion_index], collection[insertion_index - 1] = collection[insertion_index - 1], collection[insertion_index]
            insertion_index -= 1
return collection
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print(insertion_sort(unsorted))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4. 希尔排序
4.1 算法步骤
选择一个增量序列 t1,t2,……,tk,其中 ti > tj, tk = 1;
按增量序列个数 k,对序列进行 k 趟排序;
每趟排序,根据对应的增量 ti,将待排序列分割成若干长度为 m 的子序列,分别对各	子表进行直接插入排序。仅增量因子为 1时,整个序列作为一个表来处理,表长度即为整个序列的长度。
利用的性质:N间隔有序的序列,在执行M间隔(M<N)排序后仍然是M间隔有序的序列。
原始的希尔排序增量间隔:
Dm=N/2	初始间隔
Dk=D(k+1)/2
此时的时间复杂度:O(n2)
Hibbard增量序列:Dk=2k -1
此时的最坏时间复杂度:O(N*3/2)
还有Sedgewick[1,5,19,41,109…]
def shell_sort(collection):
    """Pure implementation of shell sort algorithm in Python
    :param collection:  Some mutable ordered collection with heterogeneous
    comparable items inside
    :return:  the same collection ordered by ascending
    >>> shell_sort([0, 5, 3, 2, 2])
    [0, 2, 2, 3, 5]
    >>> shell_sort([])
    []
    >>> shell_sort([-2, -5, -45])
    [-45, -5, -2]
    """
    # Marcin Ciura's gap sequence
    gaps = [701, 301, 132, 57, 23, 10, 4, 1]
    for gap in gaps:
        i = gap
        while i < len(collection):
            temp = collection[i]
            j = i
            while j >= gap and collection[j - gap] > temp:
                collection[j] = collection[j - gap]
                j -= gap
            collection[j] = temp
            i += 1
return collection
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print(shell_sort(unsorted))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
5. 堆排序
5.1 算法步骤
创建一个堆 H[0……n-1];
把堆首(最大值)和堆尾互换;
把堆的尺寸缩小 1,并调用 shift_down(0),目的是把新的数组顶端数据调整到相应位置;
重复步骤 2,直到堆的尺寸为 1。
def heapify(unsorted, index, heap_size):
    largest = index
    left_index = 2 * index + 1
    right_index = 2 * index + 2
    if left_index < heap_size and unsorted[left_index] > unsorted[largest]:
        largest = left_index
    if right_index < heap_size and unsorted[right_index] > unsorted[largest]:
        largest = right_index
    if largest != index:
        unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest]
        heapify(unsorted, largest, heap_size)
def heap_sort(unsorted):
    n = len(unsorted)
    for i in range(n // 2 - 1, -1, -1):
        heapify(unsorted, i, n)
    for i in range(n - 1, 0, -1):
        unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
        heapify(unsorted, 0, i)
    return unsorted
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print(heap_sort(unsorted))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
6、归并排序
算法步骤
申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列;
设定两个指针,最初位置分别为两个已经排序序列的起始位置;
比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置;
重复步骤 3 直到某一指针达到序列尾;将另一序列剩下的所有元素直接复制到合并序列尾。
复杂度O(NlogN) ,稳定,但是需要开辟额外空间,因此一般用在外排序中。
def merge_sort(collection):
def merge(left, right):
        result = []
        while left and right:
            result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
        return result + left + right
    if len(collection) <= 1:
        return collection
    mid = len(collection) // 2
    return merge(merge_sort(collection[:mid]), merge_sort(collection[mid:]))
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print(*merge_sort(unsorted), sep=',')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
7、快速排序
从数列中挑出一个元素,称为 “基准”(pivot);
重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。 在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序;
快速排序和归并排序都是采用分而治之的策略,快速排序的重点在于主元的选取,一般采用待排序列首位中间三个元素的中位数作为主元,快速排序更加适用于大规模数据的排序,故我们可以在递归快排之前判断数据规模,当数据量较小时选择其他排序算法(插入排序)
def median(collection, left, right):
    mid = (left + right) // 2
    if collection[left] > collection[mid]:
            collection[left], collection[mid] = collection[mid], collection[left]
    if collection[mid] > collection[right]:
            collection[mid], collection[right] = collection[right], collection[mid]
    if collection[left] > collection[right]:
            collection[left], collection[right] = collection[right], collection[left]
    collection[mid], collection[right - 1] = collection[right - 1], collection[mid]
    pivot = collection[right-1]
    return pivot
def quick_sort(collection,left,right):
    if (right-left) < 1:
        return
    pivot = median(collection,left,right)
    i = left
    j = right-2
    while True:
        while collection[i] < pivot:i+=1
        while j>0 and collection[j] > pivot:j-=1
        if i < j :
            collection[i] , collection[j] = collection[j] , collection[i]
        else:
            break
    collection[i] , collection[right-1] = collection[right-1] , collection[i]
    quick_sort(collection,left,i-1)
    quick_sort(collection,i+1,right)
if __name__ == '__main__':
    user_input = input('Enter numbers separated by a comma:\n').strip()
    unsorted = [ int(item) for item in user_input.split(',') ]
    N = len(unsorted)
    quick_sort(unsorted,0,N-1)
    print(unsorted)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
8、桶排序
算法步骤
设置固定数量的空桶。
把数据放到对应的桶中。
对每个不为空的桶中数据进行排序。
拼接不为空的桶中数据,得到结果
应用场景:把一个学校两万人的成绩排名(成绩取整0~100),可以建立一百个桶,将学生的成绩扔进桶里,然后按照桶的序号从小到大依次取出元素;如果建桶时为一个区间,可以在每个桶里面使用其他排序算法进行排序(eg:插入排序)`
DEFAULT_BUCKET_SIZE = 5
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
    if len(my_list) == 0:
        raise Exception("Please add some elements in the array.")
    min_value, max_value = (min(my_list), max(my_list))
    bucket_count = ((max_value - min_value) // bucket_size + 1)
    buckets = [[] for _ in range(int(bucket_count))]
    for i in range(len(my_list)):
        buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i])
    return sorted([buckets[i][j] for i in range(len(buckets))
                   for j in range(len(buckets[i]))])
if __name__ == "__main__":
    user_input = input('Enter numbers separated by a comma:').strip()
    unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0]
    print(bucket_sort(unsorted))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
9、基数排序
算法步骤
将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零
从最低位开始,依次进行一次排序
从最低位排序一直到最高位排序完成以后, 数列就变成一个有序序列
eg:将0-9999的50个整数排序,我们可以建立(0-9)十个桶,可以按照次位优先的原则对数列进行排序,个位–>十位–>百位—>千位。然后按照从零到九的桶序号依次取出数字即可。当然基数排序还可以用于其他方面:多关键字排序(将一副扑克牌按照黑红花块从小到大的顺序排列)
设元素个数为N,整数进制为B,LSD的趟数为P,则最坏时间复杂度是
O(P(N+B))
def radix_sort(lst):
    R = 10
    placement = 1
    # get the maximum number
    max_digit = max(lst)
    while placement < max_digit:
      # declare and initialize buckets
      buckets = [list() for _ in range( R )]
      # split lst between lists
      for i in lst:
        tmp = int((i / placement) % R)
        buckets[tmp].append(i)
      # empty lists into lst array
      a = 0
      for b in range( R ):
        buck = buckets[b]
        for i in buck:
          lst[a] = i
          a += 1
      # move to next
      placement *= R
if __name__ == "__main__":
    user_input = input('Enter numbers separated by a comma:').strip()
    unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0]
    radix_sort(unsorted)
    print(unsorted)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
十、计数排序
算法步骤花O(n)的时间扫描一下整个序列 A,获取最小值 min 和最大值 max
开辟一块新的空间创建新的数组 B,长度为 ( max - min + 1)
数组 B 中 index 的元素记录的值是 A 中某元素出现的次数
最后输出目标整数序列,具体的逻辑是遍历数组B,输出相应元素以及对应的个数
--------------------- 
 
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号