added my shell sort exercise solution code by RohanSaxena2020 · Pull Request #91 · codebasics/data-structures-algorithms-python · GitHub
Skip to content
105 changes: 105 additions & 0 deletions 1.py
84 changes: 84 additions & 0 deletions 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# ### Bubble Sort Exercise

# Modify [bubble_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
# ```
# elements = [
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
# ]
# ```
# bubble_sort function should take key from a transaction record and sort the list as per that key. For example,
# ```
# bubble_sort(elements, key='transaction_amount')
# ```
# This will sort elements by transaction_amount and your sorted list will look like,
# ```
# elements = [
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
# ]
# ```
# But if you call it like this,
# ```
# bubble_sort(elements, key='name')
# ```
# output will be,
# ```
# elements = [
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
# ]
# ```

# base bubble_sort. you can use this to sort strings too
def bubble_sort(elements):
size = len(elements)

for i in range(size-1):
swapped = False
for j in range(size-1-i):
if elements[j] > elements[j+1]:
tmp = elements[j]
elements[j] = elements[j+1]
elements[j+1] = tmp
swapped = True

if not swapped:
break

def bubble_sort_by_key(elements, key):
size = len(elements)

for i in range(size-1):
swapped = False
for j in range(size-1-i):
if elements[j][key] > elements[j+1][key]:
tmp = elements[j]
elements[j] = elements[j+1]
elements[j+1] = tmp
swapped = True

if not swapped:
break


elements = [5,9,2,1,67,34,88,34]
elements = [1,2,3,4,2]
elements = ["mona", "dhaval", "aamir", "tina", "chang"]

bubble_sort(elements)
print(elements)

elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
{ 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
{ 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
]
bubble_sort_by_key(elements2,key='transaction_amount')
print(elements2)
66 changes: 66 additions & 0 deletions 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
def swap(a, b, arr):
if a!=b:
tmp = arr[a]
arr[a] = arr[b]
arr[b] = tmp

# Sorts a (portion of an) array, divides it into partitions, then sorts those
def quicksort(A, lo, hi):
if lo >= 0 and lo < hi:
lt, gt = partition(A, lo, hi) # Multiple return values
quicksort(A, lo, lt - 1)
quicksort(A, gt + 1, hi)

# Divides array into three partitions
def partition(A, lo, hi):
# Pivot value
pivot = A[(lo + hi) // 2] # Choose the middle element as the pivot (integer division)

# Lesser, equal and greater index
lt = lo
eq = lo
gt = hi

# Iterate and compare all elements with the pivot

while eq <= gt:
if A[eq] < pivot:
# Swap the elements at the equal and lesser indices
swap(eq, lt, A)
# Increase lesser index
lt += 1
# Increase equal index
eq += 1
elif A[eq] > pivot:
# Swap the elements at the equal and greater indices
swap(eq, gt, A)
# Decrease greater index
gt -= 1
else: # A[eq] == pivot
# Increase equal index
eq += 1

# Return lesser and greater indices
return lt, gt

elements = [11,9,29,7,2,15,28]
# elements = ["mona", "dhaval", "aamir", "tina", "chang"]
quicksort(elements, 0, len(elements)-1)
print(elements)

tests = [
[11,9,29,7,2,15,28],
[3, 7, 9, 11],
[25, 22, 21, 10],
[29, 15, 28],
[],
[6]
]

try:
# Your script's entry point, e.g., function calls
for elements in tests:
quicksort(elements, 0, len(elements)-1)
print(f'sorted array: {elements}')
except Exception as e:
print(f"Error occurred: {e}")
44 changes: 44 additions & 0 deletions 4.py
Loading