快排 python3

Class Solution:
    def QuickSort(self, array, low, high):
        if low >= high:
            return
        i = low, j = high, index = array[i]    #取数组最左侧数值作为基准
        while(i<j):
            while((i<j) and (array[j] >= index)):    #从右向左寻找第一个小于基准的数值
                j -= 1
            if i<j:
                array[i] = array[j]    #将该值填到左侧,i值右移【右侧该值位置空出】
                i +=1
            while((i<j) and (array[i] < index):    #继续从左向右寻找第一个大于基准的数值
                i += 1
            if i<j:
                array[j] = array[i]    #将该值填到右侧空位,j值左移【左侧该值位置空出】
                j -=1
        array[i] = index    #将基准值填入中间空位
        QuickSort(array,low,i-1)    #分治、迭代实现
        QuickSort(array,i+1,high)
            
全部评论

相关推荐

代码飞升AL:同学院本 你这都是无效实习和跳槽 下一段底线是去一个稍微知名的公司 本质是骑驴找马 你这一直骑驴换来换去没什么区别
双非有机会进大厂吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务