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/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]), diff --git a/code/12-sorting-algorithms/insertion-sort.py b/code/12-sorting-algorithms/insertion-sort.py new file mode 100644 index 0000000..32cbd0a --- /dev/null +++ b/code/12-sorting-algorithms/insertion-sort.py @@ -0,0 +1,26 @@ +# Python program for implementation of Insertion Sort + +# Function: 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