From 265996e564c9c737772738db022506176a80c981 Mon Sep 17 00:00:00 2001 From: beppeb Date: Mon, 20 Oct 2025 13:35:11 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20quicksort.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quicksort.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 quicksort.py diff --git a/quicksort.py b/quicksort.py new file mode 100644 index 0000000..df90334 --- /dev/null +++ b/quicksort.py @@ -0,0 +1,13 @@ +def quick_sort(arr): + if len(arr) <= 1: + return arr + pivot = arr[len(arr) // 2] + left = [x for x in arr if x < pivot] + middle = [x for x in arr if x == pivot] + right = [x for x in arr if x > pivot] + return quick_sort(left) + middle + quick_sort(right) + + +nums = [3, 6, 8, 10, 1, 2, 1] +print("排序前:", nums) +print("排序后:", quick_sort(nums)) \ No newline at end of file