From a896eb8d393924da91a5b1baf47beda593178941 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 14 Jan 2021 08:37:02 +0800 Subject: [PATCH] optimization shell sort --- sorts/shell_sort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index cbd95ff..fb8aa30 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -28,7 +28,8 @@ def shell_sort(array): while j >= 0 and insert_value < array[j]: array[j + gap] = array[j] j -= gap - array[j + gap] = insert_value + if j != i - gap: + array[j + gap] = insert_value gap >>= 1 return array