Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions code/12-sorting-algorithms/bubble-sorting.py
Original file line number Diff line number Diff line change
@@ -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]),
26 changes: 26 additions & 0 deletions code/12-sorting-algorithms/insertion-sort.py
Original file line number Diff line number Diff line change
@@ -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])
43 changes: 43 additions & 0 deletions code/12-sorting-algorithms/quick-sort.py
Original file line number Diff line number Diff line change
@@ -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]),