



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A coding challenge focused on implementing four fundamental sorting algorithms in python: insertion sort, bubble sort, merge sort, and quick sort. Each task provides a clear description of the algorithm and prompts the user to write the corresponding python code. The document encourages users to explain their code, analyze their decisions, and explore potential improvements. This challenge is suitable for students learning about sorting algorithms and data structures in computer science.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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):
for i in range(1, len(list)): pointer = i
while list[pointer] < list[pointer - 1] and pointer > 0:
tmp = list[pointer] list[pointer] = list[pointer - 1] list[pointer - 1] = tmp pointer = pointer - 1
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):
leftPointer = 0 rightPointer = 0 sortedList = [] # This will hold the merged and sorted result
while leftPointer < len(leftList) and rightPointer < len(rightList):
if leftList[leftPointer] < rightList[rightPointer]:
sortedList.append(leftList[leftPointer]) leftPointer += 1 # Move the pointer in the left list else:
sortedList.append(rightList[rightPointer]) rightPointer += 1 # Move the pointer in the right list
while leftPointer < len(leftList): sortedList.append(leftList[leftPointer]) leftPointer += 1 # Move the pointer in the left list
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.