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?
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步