From 6a6349bdb0ac3d04f12b98248b27337e2ccd7108 Mon Sep 17 00:00:00 2001 From: YasirChoudhary Date: Sun, 7 Oct 2018 14:32:48 +0530 Subject: [PATCH 1/2] Optimised for loop iteration --- sorts/bubble_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index e28e7aa4f..fc1f9e537 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -31,9 +31,9 @@ def bubble_sort(collection): [-45, -5, -2] """ length = len(collection) - for i in range(length): + for i in range(length-1): swapped = False - for j in range(length-1): + for j in range(length-1-i): if collection[j] > collection[j+1]: swapped = True collection[j], collection[j+1] = collection[j+1], collection[j] From 98cc298e61a300493fd2447922908494d028315f Mon Sep 17 00:00:00 2001 From: YasirChoudhary Date: Sun, 7 Oct 2018 14:33:56 +0530 Subject: [PATCH 2/2] Optimised for loop iteration --- sorts/selection_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index 432d14090..c1d66c5a8 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -31,7 +31,7 @@ def selection_sort(collection): """ length = len(collection) - for i in range(length): + for i in range(length - 1): least = i for k in range(i + 1, length): if collection[k] < collection[least]: