九. sort 6 Nuts & Bolts Problem

Idea: quick sort

There are 3 things we have to know:

  1. nuts and bolts can not be compared inside. Only nuts[a] and bolts[b] are allowed to be compared.
  2. It's not a simple comparison with >, < or =. We are required to use 'cmp', a function from Class Comparator.
  3. A fixed order is in the () of cmp, 'Compare.cmp(a, b) to compare nuts "a" and bolts "b"', which means cmp(bolts[a], nuts[b]) is invalid.
"""
class Comparator:
    def cmp(self, a, b)
You can use Compare.cmp(a, b) to compare nuts "a" and bolts "b",
if "a" is bigger than "b", it will return 1, else if they are equal,
it will return 0, else if "a" is smaller than "b", it will return -1.
When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid.
"""


class Solution:
    # @param nuts: a list of integers
    # @param bolts: a list of integers
    # @param compare: a instance of Comparator
    # @return: nothing
    def sortNutsAndBolts(self, nuts, bolts, compare):
        # write your code here
        
        def quick_sort(low, high, nuts, bolts):
            if low < high:
                pivot = find_pivot(low, high, nuts, bolts)
                quick_sort(low, pivot-1, nuts, bolts)
                quick_sort(pivot+1, high, nuts, bolts)
        
        def find_pivot(low, high, nuts, bolts):
            pivot = high
            left_wall = low
            i = low
            while i < high:
                if compare.cmp(nuts[pivot],bolts[i]) == 1:
                    bolts[i], bolts[left_wall] = bolts[left_wall], bolts[i]
                    left_wall += 1 
                if compare.cmp(nuts[pivot], bolts[i]) == 0:
                    bolts[high], bolts[i] = bolts[i], bolts[high]
                    i -=1
                i +=1
            bolts[left_wall], bolts[high] = bolts[high], bolts[left_wall]
            
            pivot = left_wall
            left_wall = low
            i = low
            while i < high:
                if compare.cmp(nuts[i], bolts[pivot]) == -1:
                    nuts[i], nuts[left_wall] = nuts[left_wall], nuts[i]
                    left_wall += 1 
                i += 1
            nuts[left_wall], nuts[high] = nuts[high], nuts[left_wall]
            # 'return pivot' has the same effect.
            return left_wall
            
        quick_sort(0, len(nuts)-1, nuts, bolts)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,097评论 0赞 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 13,253评论 3赞 20
  • 前不久回到学校,吃饭的时候远远看到我的课代表,我心里特别激动与高兴,然而我竟一时半会儿想不起他的名字,看来真的好久...
    polly1961阅读 362评论 0赞 0
  • 我还是把我超爱的雯放在开头! 现在的我也许是在最年起最欢快的年纪,但也许在我自己未来选择的起点——就是我未来的雏形...
    木少呀呀阅读 271评论 0赞 0
  • 前言 安卓程序打个apk的包,发布到服务器,谁想用下载就可以了。你这个iOS怎么这么麻烦?还得发布,怎么给你测?....
    Liusr阅读 2,307评论 -1赞 1

友情链接更多精彩内容