From a1ea0c198cf219195816234e6673ddbdee98246e Mon Sep 17 00:00:00 2001 From: waleska Date: Thu, 1 Oct 2020 13:48:56 +0200 Subject: [PATCH 1/3] bubble sorting added --- code/12-sorting-algorithms/bubble-sorting.py | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 code/12-sorting-algorithms/bubble-sorting.py diff --git a/code/12-sorting-algorithms/bubble-sorting.py b/code/12-sorting-algorithms/bubble-sorting.py new file mode 100644 index 0000000..eb34fae --- /dev/null +++ b/code/12-sorting-algorithms/bubble-sorting.py @@ -0,0 +1,28 @@ +# Python program for implementation of Bubble Sort + +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n - 1): + # range(n) also work but outer loop will repeat one time more than needed. + + # Last i elements are already in place + for j in range(0, n - i - 1): + + # traverse the array from 0 to n-i-1 + # Swap if the element found is greater + # than the next element + if arr[j] > arr[j + 1]: + arr[j], arr[j + 1] = arr[j + 1], arr[j] + + # Driver code to test above + + +arr = [64, 34, 25, 12, 22, 11, 90] + +bubbleSort(arr) + +print("Sorted array is:") +for i in range(len(arr)): + print("%d" % arr[i]), From 29ac8d1ade6bd96173a3c4f13dc9b0bb21ce7e45 Mon Sep 17 00:00:00 2001 From: waleska Date: Thu, 1 Oct 2020 13:52:22 +0200 Subject: [PATCH 2/3] insertion sort added --- .gitignore | 1 + code/12-sorting-algorithms/insertion-sort.py | 26 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 code/12-sorting-algorithms/insertion-sort.py diff --git a/.gitignore b/.gitignore index d8f641b..50f3135 100644 --- a/.gitignore +++ b/.gitignore @@ -122,6 +122,7 @@ code/09-working-with-files/.idea/vagrant.xml code/09-working-with-files/.idea/vcs.xml code/09-working-with-files/.idea/inspectionProfiles/profiles_settings.xml misc.xml +.idea .idea/$CACHE_FILE$ .idea/beginners-course.iml .idea/modules.xml diff --git a/code/12-sorting-algorithms/insertion-sort.py b/code/12-sorting-algorithms/insertion-sort.py new file mode 100644 index 0000000..8a64468 --- /dev/null +++ b/code/12-sorting-algorithms/insertion-sort.py @@ -0,0 +1,26 @@ +# Python program for implementation of Insertion Sort + +# Function to do insertion sort +def insertionSort(arr): + # Traverse through 1 to len(arr) + for i in range(1, len(arr)): + + key = arr[i] + + # Move elements of arr[0..i-1], that are + # greater than key, to one position ahead + # of their current position + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + # Driver code to test above + + +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print("Sorted array is:") +for i in range(len(arr)): + print("%d" % arr[i]) \ No newline at end of file From aec291be389ce052645cd6f622c116dd95aa88a4 Mon Sep 17 00:00:00 2001 From: waleska Date: Thu, 1 Oct 2020 13:54:13 +0200 Subject: [PATCH 3/3] insertion sort --- code/12-sorting-algorithms/insertion-sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/12-sorting-algorithms/insertion-sort.py b/code/12-sorting-algorithms/insertion-sort.py index 8a64468..32cbd0a 100644 --- a/code/12-sorting-algorithms/insertion-sort.py +++ b/code/12-sorting-algorithms/insertion-sort.py @@ -1,6 +1,6 @@ # Python program for implementation of Insertion Sort -# Function to do insertion sort +# Function: insertion sort def insertionSort(arr): # Traverse through 1 to len(arr) for i in range(1, len(arr)):