zoukankan      html  css  js  c++  java
  • QuickSort again

      I wrote a blog about Quick Sort before. Let's talk more about it.

      If you don't think that the implementation in the blog mentioned is easy to remember and understand.

    You can send your implementation to me by E-mail(lxw.ucas@gmail.com). Now let's look at some other

    implementations that are not so amazing:

      A new implementation in python is shown here:

    def qs(arr):
        if not arr:
            return [] 
        low = []
        high = []
        for x in arr[1:]:
            if x <= arr[0]:
                low.append(x)
            else:
                high.append(x)
        low = qs(low)
        high = qs(high)
        return low + arr[:1] + high    # low + arr[0] + high   is NOT OK! --> ERROR PROMPT: 'can only concatenate list(not int) to list'

      It seemed not so amazing and easy to understand as the former one. But it's ok as well. : ) Let's look

    at another new implementation in python:

    def quickSort(arr, start, end):
        if start >= end:    # EXIT
            return
        i = start
        j = end
        target = arr[i]
        while i < j:
            while arr[i] <= target and i < end:
                i += 1
            while arr[j] > target and j > start:
                j -= 1
            if i < j:
                arr[i], arr[j] = arr[j], arr[i]
        arr[start] = arr[j]
        arr[j] = target
        quickSort(arr, start, j - 1)
        quickSort(arr, j + 1, end)

      Now, which way of implementation do you prefer? Do you think the implementation in the blog mentioned

    is easy to understand and remember? 

  • 相关阅读:
    假期(面试题二)
    假期(面向对象相关)
    假期(模块相关)
    假期(面试题一)
    假期(函数相关)
    最后一个假期
    Django缓存问题
    python pass关键字神奇吗
    python中类变量,成员变量
    python类中self是什么
  • 原文地址:https://www.cnblogs.com/lxw0109/p/QuickSort-again.html
Copyright © 2011-2022 走看看