From 34ee1a2822e3c81ba7a2cbbc46facdaf47ca8fc5 Mon Sep 17 00:00:00 2001 From: Satish Mishra Date: Wed, 29 Jun 2022 18:59:06 +0530 Subject: [PATCH 1/6] Add Optimized Shell Sort --- sorts/shrink_shell.py | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 sorts/shrink_shell.py diff --git a/sorts/shrink_shell.py b/sorts/shrink_shell.py new file mode 100644 index 000000000000..b4421a876927 --- /dev/null +++ b/sorts/shrink_shell.py @@ -0,0 +1,66 @@ +""" +This function implements the shell sort algorithm. + +Shell sort is a sorting algorithm that works by first sorting +elements that are far apart from each other, and then sorting +elements that are closer together. This reduces the amount of +swapping that needs to be done, and makes the algorithm more +efficient. + +The algorithm works by first choosing a 'gap' value. This value +determines how far apart the elements will be that are sorted +first. The gap is then decreased, and the elements are sorted +again. This process is repeated until the gap is 1, at which +point the elements are sorted using insertion sort. + +Shell sort is an efficient algorithm that is easy to implement. +It is a good choice for sorting large arrays of data. + +""" + +def shell_sort(collection): + """Implementation of shell sort algorithm in Python + :param collection: Some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + >>> shell_sort([3, 2, 1]) + [1, 2, 3] + >>> shell_sort([]) + [] + >>> shell_sort([1]) + [1] + """ + + # Choose an initial gap value + gap = len(collection) + + # Set the gap value to be decreased by a factor of 1.3 + # after each iteration + shrink = 1.3 + + # Continue sorting until the gap is 1 + while gap > 1: + + # Decrease the gap value + gap = int(gap / shrink) + + # Sort the elements using insertion sort + for i in range(gap, len(collection)): + temp = collection[i] + j = i + while j >= gap and collection[j - gap] > temp: + collection[j] = collection[j - gap] + j -= gap + collection[j] = temp + + return collection + + +if __name__ == "__main__": + import doctest + doctest.testmod() + + user_input = input("Enter numbers separated by a comma:\n").strip() + collection = [int(item) for item in user_input.split(",")] + print(shell_sort(collection)) \ No newline at end of file From d7eb29e998a7b8bb9dca549dd22778746043e59f Mon Sep 17 00:00:00 2001 From: Satish Mishra Date: Wed, 29 Jun 2022 19:32:55 +0530 Subject: [PATCH 2/6] Added return type --- sorts/shrink_shell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/shrink_shell.py b/sorts/shrink_shell.py index b4421a876927..36b3e50be253 100644 --- a/sorts/shrink_shell.py +++ b/sorts/shrink_shell.py @@ -18,7 +18,7 @@ """ -def shell_sort(collection): +def shell_sort(collection: list) -> list: """Implementation of shell sort algorithm in Python :param collection: Some mutable ordered collection with heterogeneous comparable items inside From 5f6eb332782d6c1c7db3a37e0824e825d79b964a Mon Sep 17 00:00:00 2001 From: Satish Mishra Date: Fri, 1 Jul 2022 03:58:09 +0530 Subject: [PATCH 3/6] reformatted --- sorts/shrink_shell.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sorts/shrink_shell.py b/sorts/shrink_shell.py index 36b3e50be253..69992bfb75bc 100644 --- a/sorts/shrink_shell.py +++ b/sorts/shrink_shell.py @@ -1,29 +1,32 @@ """ -This function implements the shell sort algorithm. +This function implements the shell sort algorithm +which is slightly faster than its pure implementation. -Shell sort is a sorting algorithm that works by first sorting -elements that are far apart from each other, and then sorting -elements that are closer together. This reduces the amount of -swapping that needs to be done, and makes the algorithm more -efficient. +This shell sort is implemented using a gap, which +shrinks by a certain factor each iteration. In this +implementation, the gap is initially set to the +length of the collection. The gap is then reduced by +a certain factor (1.3) each iteration. -The algorithm works by first choosing a 'gap' value. This value -determines how far apart the elements will be that are sorted -first. The gap is then decreased, and the elements are sorted -again. This process is repeated until the gap is 1, at which -point the elements are sorted using insertion sort. - -Shell sort is an efficient algorithm that is easy to implement. -It is a good choice for sorting large arrays of data. +For each iteration, the algorithm compares elements +that are a certain number of positions apart +(determined by the gap). If the element at the higher +position is greater than the element at the lower +position, the two elements are swapped. The process +is repeated until the gap is equal to 1. +The reason this is more efficient is that it reduces +the number of comparisons that need to be made. By +using a smaller gap, the list is sorted more quickly. """ + def shell_sort(collection: list) -> list: """Implementation of shell sort algorithm in Python :param collection: Some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending - + >>> shell_sort([3, 2, 1]) [1, 2, 3] >>> shell_sort([]) @@ -59,8 +62,5 @@ def shell_sort(collection: list) -> list: if __name__ == "__main__": import doctest - doctest.testmod() - user_input = input("Enter numbers separated by a comma:\n").strip() - collection = [int(item) for item in user_input.split(",")] - print(shell_sort(collection)) \ No newline at end of file + doctest.testmod() From f9d7ed1b6197e5fd0da70ad26e8471886b57a687 Mon Sep 17 00:00:00 2001 From: Satish Mishra Date: Fri, 1 Jul 2022 03:59:18 +0530 Subject: [PATCH 4/6] added shrink_shell.py --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d30e275d067f..85bdc928464b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -973,6 +973,7 @@ * [Recursive Quick Sort](sorts/recursive_quick_sort.py) * [Selection Sort](sorts/selection_sort.py) * [Shell Sort](sorts/shell_sort.py) + * [Shrink Shell Sort](sorts/shrink_shell.py) * [Slowsort](sorts/slowsort.py) * [Stooge Sort](sorts/stooge_sort.py) * [Strand Sort](sorts/strand_sort.py) From 812cac02c14ef9eaa75f07b9dfa1244d2e36330d Mon Sep 17 00:00:00 2001 From: Satish Mishra Date: Mon, 4 Jul 2022 16:34:19 +0530 Subject: [PATCH 5/6] ran directory generator --- DIRECTORY.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 85bdc928464b..7b486f4538a0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -444,7 +444,6 @@ * [Scoring Functions](machine_learning/scoring_functions.py) * [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py) * [Similarity Search](machine_learning/similarity_search.py) - * [Support Vector Machines](machine_learning/support_vector_machines.py) * [Word Frequency Functions](machine_learning/word_frequency_functions.py) ## Maths @@ -910,6 +909,7 @@ ## Scheduling * [First Come First Served](scheduling/first_come_first_served.py) + * [Highest Response Ratio Next](scheduling/highest_response_ratio_next.py) * [Multi Level Feedback Queue](scheduling/multi_level_feedback_queue.py) * [Non Preemptive Shortest Job First](scheduling/non_preemptive_shortest_job_first.py) * [Round Robin](scheduling/round_robin.py) @@ -973,7 +973,7 @@ * [Recursive Quick Sort](sorts/recursive_quick_sort.py) * [Selection Sort](sorts/selection_sort.py) * [Shell Sort](sorts/shell_sort.py) - * [Shrink Shell Sort](sorts/shrink_shell.py) + * [Shrink Shell](sorts/shrink_shell.py) * [Slowsort](sorts/slowsort.py) * [Stooge Sort](sorts/stooge_sort.py) * [Strand Sort](sorts/strand_sort.py) @@ -1017,6 +1017,7 @@ * [Reverse Words](strings/reverse_words.py) * [Split](strings/split.py) * [Upper](strings/upper.py) + * [Wave](strings/wave.py) * [Wildcard Pattern Matching](strings/wildcard_pattern_matching.py) * [Word Occurrence](strings/word_occurrence.py) * [Word Patterns](strings/word_patterns.py) From 5d5494bf5d614d6918a51afd39fdbb57608cee32 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 14 Sep 2022 09:38:31 +0100 Subject: [PATCH 6/6] Rename shrink_shell.py to shrink_shell_sort.py --- sorts/{shrink_shell.py => shrink_shell_sort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorts/{shrink_shell.py => shrink_shell_sort.py} (100%) diff --git a/sorts/shrink_shell.py b/sorts/shrink_shell_sort.py similarity index 100% rename from sorts/shrink_shell.py rename to sorts/shrink_shell_sort.py