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

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

2023/2024

Uploaded on 12/17/2024

fahad-14
fahad-14 🇬🇧

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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.