From a1ea0c198cf219195816234e6673ddbdee98246e Mon Sep 17 00:00:00 2001 From: waleska Date: Thu, 1 Oct 2020 13:48:56 +0200 Subject: [PATCH 1/4] 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/4] 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/4] 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)): From 342b7e13e5cd99a1c8c0460c8549d6dbf4e75cc1 Mon Sep 17 00:00:00 2001 From: waleska Date: Thu, 1 Oct 2020 13:58:08 +0200 Subject: [PATCH 4/4] quicksort added --- code/12-sorting-algorithms/quick-sort.py | 43 ++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 code/12-sorting-algorithms/quick-sort.py diff --git a/code/12-sorting-algorithms/quick-sort.py b/code/12-sorting-algorithms/quick-sort.py new file mode 100644 index 0000000..e5608f9 --- /dev/null +++ b/code/12-sorting-algorithms/quick-sort.py @@ -0,0 +1,43 @@ +# Python: Quicksort Sort + +def partition(arr, low, high): + i = (low - 1) # index of smaller element + pivot = arr[high] # pivot + + for j in range(low, high): + + # If current element is smaller than or + # equal to pivot + if arr[j] <= pivot: + # increment index of smaller element + i = i + 1 + arr[i], arr[j] = arr[j], arr[i] + + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return (i + 1) + + +# Function to do Quick sort + + +def quickSort(arr, low, high): + if len(arr) == 1: + return arr + if low < high: + # pi is partitioning index, arr[p] is now + # at right place + pi = partition(arr, low, high) + + # Separately sort elements before + # partition and after partition + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) + + +# Driver code to test above +arr = [10, 7, 8, 9, 1, 5] +n = len(arr) +quickSort(arr, 0, n - 1) +print("Sorted array is:") +for i in range(n): + print("%d" % arr[i]), \ No newline at end of file