Docsity

Coding Challenge: Insertion, Bubble, Merge and Quick Sort Algorithms in Python, Study notes of Computer science

Toggle sidebar
bg1
Coding Challenge: Insertion, Bubble, Merge and Quick Sort Algorithms
Task One:
Write the Python code for the following Insertion Sort:
Copy and paste your solution below:
And explain your solution, why did you make the decisions you made and how could you improve
your program?
def insertionSort(list):
# Iterate through each value of the list
for i in range(1, len(list)):
pointer = i
# Slide value on the left to insert it in position
while list[pointer] < list[pointer - 1] and pointer > 0:
# Swap values with the previous value
tmp = list[pointer]
list[pointer] = list[pointer - 1]
list[pointer - 1] = tmp
pointer = pointer - 1
# Print list at the end of each iteration
print(list)
list2 = [4,5,10,6,2,100]
eg insertionSort(list2)
pf3
pf4
pf5

Partial preview of the text

Download Coding Challenge: Insertion, Bubble, Merge and Quick Sort Algorithms in Python and more Study notes Computer science in PDF only on Docsity!

Coding Challenge: Insertion, Bubble, Merge and Quick Sort Algorithms

Task One: Write the Python code for the following Insertion Sort: Copy and paste your solution below: And explain your solution, why did you make the decisions you made and how could you improve your program? def insertionSort(list):

Iterate through each value of the list

for i in range(1, len(list)): pointer = i

Slide value on the left to insert it in position

while list[pointer] < list[pointer - 1] and pointer > 0:

Swap values with the previous value

tmp = list[pointer] list[pointer] = list[pointer - 1] list[pointer - 1] = tmp pointer = pointer - 1

Print list at the end of each iteration

print(list) list2 = [4,5,10,6,2,100] eg insertionSort(list2)

Task Two Write the Python code for the following Bubble Sort:

Task Three: Write a Python program for the following Merge Sort. Copy and paste your solution below, and explain the decisions you made and why. def merge(leftList, rightList):

Initialize pointers for both left and right lists

leftPointer = 0 rightPointer = 0 sortedList = [] # This will hold the merged and sorted result

Merge the two lists until we reach the end of one of them

while leftPointer < len(leftList) and rightPointer < len(rightList):

Compare the current elements of both lists

if leftList[leftPointer] < rightList[rightPointer]:

If the element in leftList is smaller, append it to sortedList

sortedList.append(leftList[leftPointer]) leftPointer += 1 # Move the pointer in the left list else:

If the element in rightList is smaller, append it to sortedList

sortedList.append(rightList[rightPointer]) rightPointer += 1 # Move the pointer in the right list

Append any remaining elements in leftList to sortedList

while leftPointer < len(leftList): sortedList.append(leftList[leftPointer]) leftPointer += 1 # Move the pointer in the left list

Append any remaining elements in rightList to sortedList

while rightPointer < len(rightList): sortedList.append(rightList[rightPointer]) rightPointer += 1 # Move the pointer in the right list return sortedList # Return the fully merged and sorted list

Copy and paste your solution below, and explain the decisions you made and why. Check your solution against the model one on the attached txt file.