码迷,mamicode.com
首页 > 编程语言 > 详细

并排,快排和冒泡排序

时间:2019-03-05 09:36:21      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:排序   none   pivot   mergesort   +=   click   quicksort   def   append   

并排

技术图片
class mergeSort:
    def mergeSort(self, A):
        self.mSort(A, 0, len(A)-1)
        
    def mSort(self, A, lo, hi):
        if lo < hi:
            mid = (lo+hi)//2
            self.mSort(A, lo, mid)
            self.mSort(A, mid+1, hi)
            self.merge(A, lo, mid, hi)
            
    def merge(self, A, lo, mid, hi):
        l = A[lo:mid+1]
        r = A[mid+1:hi+1]
        l.append(float("inf"))
        r.append(float("inf"))
        i, j = 0, 0
        for k in range(lo, hi+1):
            if l[i] <= r[j]:
                A[k] = l[i]
                i+=1
            else:
                A[k] = r[j]
                j+=1
View Code

快排

技术图片
class quickSort:
    def quickSort(self, A):
        self.qSort(A, 0, len(A)-1)
        
    def qSort(self, A, lo, hi):
        if lo < hi:
            p = self.partition(A, lo, hi)
            self.qSort(A, lo, p-1)
            self.qSort(A, p+1, hi)
    def partition(self, A, lo, hi):
        pivot = A[hi]
        A[hi], A[lo] = A[lo], A[hi]
        bound = lo
        for i in range(lo, hi+1):
            if A[i] < pivot:
                bound += 1
                A[bound], A[i] = A[i], A[bound]
        A[lo], A[bound] = A[bound], A[lo]
        return bound
View Code

冒泡排序

技术图片
#1 4 3 2
#1 3 2 4
#1 2 3 4
def bubble_sort1(A):
    for i in range (0, len(A) - 1):
        for j in range (0, len(A) - i - 1):
            if A[j] > A[j+1]:
                A[j], A[j+1] = A[j+1], A[j]
View Code

并排,快排和冒泡排序

标签:排序   none   pivot   mergesort   +=   click   quicksort   def   append   

原文地址:https://www.cnblogs.com/Xuan-Yu95/p/10474459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!