From c65fa75832c83db62ceec4ef8c248f26dc787251 Mon Sep 17 00:00:00 2001 From: leitao Date: Mon, 9 May 2022 16:06:19 +0800 Subject: [PATCH 1/4] feat: added shell sort in Python --- basic/sorting/ShellSort/ShellSort.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 basic/sorting/ShellSort/ShellSort.py diff --git a/basic/sorting/ShellSort/ShellSort.py b/basic/sorting/ShellSort/ShellSort.py new file mode 100644 index 0000000000000..4261dec806104 --- /dev/null +++ b/basic/sorting/ShellSort/ShellSort.py @@ -0,0 +1,15 @@ +def shellSort(arr): + n = len(arr) + gap = int(n/2) + while gap > 0: + for i in range(gap,n): + temp = arr[i] + j = i + while j >= gap and arr[j-gap] >temp: + arr[j] = arr[j-gap] + j -= gap + arr[j] = temp + gap = int(gap/2) + +arr = [ 12, 34, 54, 2, 3] +shellSort(arr) From 025502b97d2ee54f3fbdf637a77af54b457c7c1e Mon Sep 17 00:00:00 2001 From: leitao Date: Mon, 9 May 2022 16:14:49 +0800 Subject: [PATCH 2/4] feat: added selection sort in Python --- basic/sorting/SelectionSort/SelectionSort.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 basic/sorting/SelectionSort/SelectionSort.py diff --git a/basic/sorting/SelectionSort/SelectionSort.py b/basic/sorting/SelectionSort/SelectionSort.py new file mode 100644 index 0000000000000..a9f7091e3d6bf --- /dev/null +++ b/basic/sorting/SelectionSort/SelectionSort.py @@ -0,0 +1,13 @@ +def selectionSort(arr): + n = len(arr) + for i in range(n - 1): + min_index = i + for j in range(i + 1, n): + if arr[j] < arr[min_index]: + min_index = j + arr[min_index], arr[i] = arr[i], arr[min_index] + +array = [26, 11, 99 , 33, 69, 77, 55, 56, 67] +selectionSort(array) + + From d291764bfcf52812c568693b4aec609ae10b0269 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Mon, 9 May 2022 16:25:08 +0800 Subject: [PATCH 3/4] Update ShellSort.py --- basic/sorting/ShellSort/ShellSort.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/basic/sorting/ShellSort/ShellSort.py b/basic/sorting/ShellSort/ShellSort.py index 4261dec806104..95c9751605449 100644 --- a/basic/sorting/ShellSort/ShellSort.py +++ b/basic/sorting/ShellSort/ShellSort.py @@ -1,15 +1,17 @@ def shellSort(arr): n = len(arr) - gap = int(n/2) + gap = int(n / 2) while gap > 0: - for i in range(gap,n): + for i in range(gap, n): temp = arr[i] j = i - while j >= gap and arr[j-gap] >temp: - arr[j] = arr[j-gap] + while j >= gap and arr[j - gap] > temp: + arr[j] = arr[j - gap] j -= gap arr[j] = temp - gap = int(gap/2) + gap = int(gap / 2) -arr = [ 12, 34, 54, 2, 3] + +arr = [12, 34, 54, 2, 3] shellSort(arr) +print(arr) From 1f30c31de093fe84b6fddcca6e51d6b2cf72d205 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Mon, 9 May 2022 16:25:50 +0800 Subject: [PATCH 4/4] Update SelectionSort.py --- basic/sorting/SelectionSort/SelectionSort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basic/sorting/SelectionSort/SelectionSort.py b/basic/sorting/SelectionSort/SelectionSort.py index a9f7091e3d6bf..7f6de8703f67c 100644 --- a/basic/sorting/SelectionSort/SelectionSort.py +++ b/basic/sorting/SelectionSort/SelectionSort.py @@ -7,7 +7,7 @@ def selectionSort(arr): min_index = j arr[min_index], arr[i] = arr[i], arr[min_index] -array = [26, 11, 99 , 33, 69, 77, 55, 56, 67] -selectionSort(array) - +arr = [26, 11, 99, 33, 69, 77, 55, 56, 67] +selectionSort(arr) +print(arr)