A sorting algorithm puts a list of elements in a particular order. In python, if you want to sort a list, you can do list.sort(), however, here we will explore how to create our own sorting function.

Some of the types of sorting include bubble sort, sort insertion sort, selection sort, merge sort, heap sort, and, quick sort. Some sorts are more efficient than others or may have different complexities. You can sort lists numerically, alphabetically, in ascending or descending order. For the coded examples below (written in python 3), I have focused on ordering numbers in ascending order.

Bubble Sort

```# bubble sort

# ascending order

def bubbleSort(numList):

for passes in range(len(numList)-1, 0, -1):

for i in range (passes):

if numList[i] > numList[i+1]:

temp = numList[i]

numList[i] = numList[i+1]

numList[i+1] = temp

numList = [5,4,3,2,1]

print(numList)

bubbleSort(numList)

print(numList)```

Selection Sort

```# selection sort

# ascending order

def selectionSort(numList):

maxPos = 0

for pos in range(1, freePos + 1):

if numList[pos] > numList[maxPos]:

maxPos = pos

temp = numList[freePos]

numList[freePos] = numList[maxPos]

numList[maxPos] = temp

numList = [5,4,3,2,1]

print(numList)

selectionSort(numList)

print(numList)```

Insertion Sort

```# insertion sort

# ascending order

def insertionSort(numList):

for pos in range(1, len(numList)):

compareNum = numList[pos]

while pos>0 and numList[pos-1]>compareNum:

numList[pos] = numList[pos-1]

pos = pos -1

numList[pos] = compareNum

numList = [5,4,3,2,1]

print(numList)

insertionSort(numList)

print(numList)```

Stay updated for the next post by following the official Instagram @bitnibblebyteblog

This post will be updated later with more sorting algorithms.