Bubble Sort
Bubble Sort
Bubble Sort
June 4, 2024
1 Bubble Sort
[2]: def bubble_sort(elements):
length = len(elements)
for i in range(length-1):
sorted = False
for j in range(length-1-i):
if elements[j] > elements[j+1]:
temp = elements[j]
elements[j] = elements[j+1]
elements[j+1] = temp
sorted = True
if not sorted:
break
[4]: elements
[4]: [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
1.1 Excercise
[42]: def bubble_sort_dict(elements, key='transaction_amount'):
length = len(elements)
for i in range(length):
for j in range(length-1):
if elements[j][key] > elements[j+1][key]:
elements[j], elements[j+1] = elements[j+1], elements[j]
1
[43]: elements = [
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
{ 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google␣
↪pixel'},
[51]: elements
2 Selection Sort
[19]: def selection_sort(elements):
length = len(elements)
for i in range(length):
min_index = i
for j in range(i+1, length):
if elements[j] < elements[min_index]:
min_index = j
if min_index != i :
elements[i], elements[min_index] = elements[min_index], elements[i]
[21]: elements
[21]: [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
2.1 Exercise
[ ]:
2
length = len(details)
if min_index != i:
details[i], details[min_index] = details[min_index], details[i]
[72]: student_list = [
{'First Name': 'Raj', 'Last Name': 'Nayyar'},
{'First Name': 'Suraj', 'Last Name': 'Sharma'},
{'First Name': 'Karan', 'Last Name': 'Kumar'},
{'First Name': 'Jade', 'Last Name': 'Canary'},
{'First Name': 'Raj', 'Last Name': 'Thakur'},
{'First Name': 'Raj', 'Last Name': 'Sharma'},
{'First Name': 'Kiran', 'Last Name': 'Kamla'},
{'First Name': 'Armaan', 'Last Name': 'Kumar'},
{'First Name': 'Jaya', 'Last Name': 'Sharma'},
{'First Name': 'Ingrid', 'Last Name': 'Galore'},
{'First Name': 'Jaya', 'Last Name': 'Seth'},
{'First Name': 'Armaan', 'Last Name': 'Dadra'},
{'First Name': 'Ingrid', 'Last Name': 'Maverick'},
{'First Name': 'Aahana', 'Last Name': 'Arora'}
]
selection_sort_dict(student_list, sort_by_list)
[73]: student_list
3
{'First Name': 'Raj', 'Last Name': 'Thakur'},
{'First Name': 'Suraj', 'Last Name': 'Sharma'}]
3 Insertion Sort
[118]: def insertion_sort(elements):
key = elements[i]
j = i - 1
j -= 1
elements[j+1] = key
key = arr[i]
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
[125]: insertion_sort(elements)
[126]: elements
[126]: [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
[ ]:
4
4 Merge Sort
[236]: def merge_sort(elements):
length = len(elements)
if length <= 1:
return elements
mid = length//2
left = elements[:mid]
right = elements[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
k += 1
[238]: merge_sort(elements)
[239]: elements
[239]: [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
5
4.1 Excercise
[198]: def merge_sort_dict(elements, key='name'):
length = len(elements)
if length <= 1:
return elements
mid = length // 2
left = elements[:mid]
right = elements[mid:]
left = merge_sort_dict(left)
right = merge_sort_dict(right)
i = j = 0
sorted_list = []
# k += 1
return sorted_list
6
[232]: def merge_sort_ex(elements, key='name'):
length = len(elements)
if length <= 1:
return
mid = length // 2
left = elements[:mid]
right = elements[mid:]
merge_sort_ex(left)
merge_sort_ex(right)
sorted_list = []
i = j = 0
sorted_list.extend(left[i:])
sorted_list.extend(left[j:])
return sorted_list
[233]: elements = [
{ 'name': 'vedanth', 'age': 17, 'time_hours': 1},
{ 'name': 'rajab', 'age': 12, 'time_hours': 3},
{ 'name': 'vignesh', 'age': 21, 'time_hours': 2.5},
{ 'name': 'chinmay', 'age': 24, 'time_hours': 1.5},
]
[235]: merge_sort_ex(elements)
7
[235]: [{'name': 'vedanth', 'age': 17, 'time_hours': 1},
{'name': 'rajab', 'age': 12, 'time_hours': 3},
{'name': 'vedanth', 'age': 17, 'time_hours': 1},
{'name': 'rajab', 'age': 12, 'time_hours': 3}]
[ ]:
5 Quick sort
[ ]:
if length <= 1:
return elements
else:
pivot = elements.pop()
lower_element = []
higher_element = []
for i in elements:
if i < pivot:
lower_element.append(i)
else:
higher_element.append(i)
[35]: quick_sort(elements)
[35]: [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
[ ]:
[ ]:
8
6 Linear Search algorithm
[30]: def linear_search(elements, n):
for i in range(len(elements)):
if elements[i] == n:
return f'Found at {i}th position'
else:
return f'{n} Not Found'
break
linear_search(elements , 100)
length = len(elements)
if length <= 1:
return elements
if elements[mid] == target:
return f"{target} found at {mid}th postion"
sorted_elements = quick_sort(elements)
9
[62]: binary_search(sorted_elements,10)
prev = step
step += int(n**0.5)
if prev >= n:
return -1
return -1
[13]: elements = [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
jump_search(elements, 6)
[13]: -1
9 Interpolation Search
[18]: def interpolation_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high and target >= arr[low] and target <= arr[high]:
if low == high:
if arr[low] == target:
return low
return 'target not found'
if arr[pos] == target:
10
return pos
[20]: elements = [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
interpolation_search(elements, 49)
10 Exponential Search
[28]: def exponential_search(arr, target):
if arr[0] == target:
return 0
i = 1
while i < len(arr) and arr[i] <= target:
i *= 2
[30]: elements = [1, 2, 5, 12, 17, 23, 43, 45, 47, 57, 73, 75, 79, 90, 102]
exponential_search(elements, 0)
11