Sorting Algorithms

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]





Selection Sort

# selection sort

# ascending order

def selectionSort(numList):

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

        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]






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]






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

This post will be updated later with more sorting algorithms.

Ada Knowe 🙂





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: