From 0ec24ba16671398ab8b2e25dfde5f813ee726cb1 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:11:09 +0530 Subject: [PATCH 1/7] Update README.md --- Data Structures and Algorithms/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data Structures and Algorithms/README.md b/Data Structures and Algorithms/README.md index ca9c727f..c00d69a0 100644 --- a/Data Structures and Algorithms/README.md +++ b/Data Structures and Algorithms/README.md @@ -8,6 +8,8 @@ - Bogo Sort +- Cocktail Sort + - Counting Sort - Linear search (for numbers) From 047f0e3d6f87a9906e5f605d515128d9c621cc07 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:12:51 +0530 Subject: [PATCH 2/7] Create cocktail_sort --- .../Sorting Algorithms/cocktail_sort | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Data Structures and Algorithms/Sorting Algorithms/cocktail_sort diff --git a/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort b/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort new file mode 100644 index 00000000..3c56e5d0 --- /dev/null +++ b/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort @@ -0,0 +1,27 @@ +import sys + +def cocktail_sort(arr): + n = len(arr) + swapped = True + start = 0 + end = n - 1 + while swapped: + swapped = False + + for i in range(start, end): + if arr[i] > arr[i + 1]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + swapped = True + if not swapped: + break + swapped = False + end -= 1 + + for i in range(end - 1, start - 1, -1): + if arr[i] > arr[i + 1]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + swapped = True + start += 1 + return arr + +print(cocktail_sort([5, 8, 1, 4, 7, 9, 6, 3, 2])) From 900644afdfa6af2d9296669cee3f0ee158e61aa9 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:14:02 +0530 Subject: [PATCH 3/7] Rename cocktail_sort to cocktail_sort.py --- .../Sorting Algorithms/{cocktail_sort => cocktail_sort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Data Structures and Algorithms/Sorting Algorithms/{cocktail_sort => cocktail_sort.py} (100%) diff --git a/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort b/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py similarity index 100% rename from Data Structures and Algorithms/Sorting Algorithms/cocktail_sort rename to Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py From d27c17628b207b9373fa291856df069e13a67c27 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:17:36 +0530 Subject: [PATCH 4/7] Create shell_sort.py --- .../Sorting Algorithms/shell_sort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Data Structures and Algorithms/Sorting Algorithms/shell_sort.py diff --git a/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py b/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py new file mode 100644 index 00000000..4e6f169f --- /dev/null +++ b/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py @@ -0,0 +1,18 @@ +def shell_sort(arr): + n = len(arr) + gap = n // 2 # gap size + + while gap > 0: + for i in range(gap, n): + temp = arr[i] + j = i + # Perform insertion sort for the elements separated by the gap + while j >= gap and arr[j - gap] > temp: + arr[j] = arr[j - gap] + j -= gap + arr[j] = temp + gap //= 2 + + return arr + +print(shell_sort([5, 8, 1, 4, 7, 9, 6, 3, 2])) From d07a4898b0ded155a64c37e727f43070fbc75115 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:18:49 +0530 Subject: [PATCH 5/7] Update README.md --- Data Structures and Algorithms/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data Structures and Algorithms/README.md b/Data Structures and Algorithms/README.md index c00d69a0..29a0afc6 100644 --- a/Data Structures and Algorithms/README.md +++ b/Data Structures and Algorithms/README.md @@ -38,6 +38,8 @@ - Recursive Binary Search +- Shell Sort + - Selection Sort - Binary Tree traversal From 75e948517ef0cb018f5db9c5531144e405e8f23b Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:24:52 +0530 Subject: [PATCH 6/7] Updated files --- .../Sorting Algorithms/cocktail_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py b/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py index 3c56e5d0..6807ce99 100644 --- a/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py +++ b/Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py @@ -1,4 +1,4 @@ -import sys +import sys def cocktail_sort(arr): n = len(arr) From 1fcf2f01b5ec96e7599056314c2710508baa5222 Mon Sep 17 00:00:00 2001 From: Shreya Venkatapathy <130058451+Shreya-0309@users.noreply.github.com> Date: Fri, 7 Jun 2024 15:25:08 +0530 Subject: [PATCH 7/7] Updated files --- Data Structures and Algorithms/Sorting Algorithms/shell_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py b/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py index 4e6f169f..d84fbb29 100644 --- a/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py +++ b/Data Structures and Algorithms/Sorting Algorithms/shell_sort.py @@ -1,6 +1,6 @@ def shell_sort(arr): n = len(arr) - gap = n // 2 # gap size + gap = n // 2 # gap size while gap > 0: for i in range(gap, n):