def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
This algorithm performs better than Bubble Sort and
Selection Sort, but it's not efficient for large datasets.
Quote: "Insertion Sort is like arranging a deck of cards, it's
simple and efficient when you have a small number of
items to sort."
Merge Sort
Merge Sort algorithm sorts an array by dividing it into half,
then sorting each half, and finally merging the sorted
halves.
Here's an example of how Merge Sort works in Python:
def mergeSort(arr):
if len(arr) >1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
mergeSort(L)
mergeSort(R)
i = j = k = 0
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
This algorithm performs better than Bubble Sort and
Selection Sort, but it's not efficient for large datasets.
Quote: "Insertion Sort is like arranging a deck of cards, it's
simple and efficient when you have a small number of
items to sort."
Merge Sort
Merge Sort algorithm sorts an array by dividing it into half,
then sorting each half, and finally merging the sorted
halves.
Here's an example of how Merge Sort works in Python:
def mergeSort(arr):
if len(arr) >1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
mergeSort(L)
mergeSort(R)
i = j = k = 0