Sorting Algorithms in Python

Naem Azam
4 min readJul 11, 2023

--

Sorting Algorithms and Searching algorithms like insertion sort, and selection sort are essential to learning as a programmer because these form the base of any programmer. As a programmer, you have to deal with large amounts of data occasionally. This data needs to be sorted in a logical manner. You will also have to search through the data set to find a unique item in the list.

Sorting Algorithms in Python

Sorting and searching can be achieved in Python through the use of simple statements and algorithms. A sorting algorithm is used to rearrange a given list or an array of elements in a defined order, either increasing or decreasing. A searching algorithm is used to check the presence of a component in a list or array and displays the position of the element in the list or array or will return a False if it is non-existent in the list. In this article, we are discussing the basic Sorting Algorithms and Searching Algorithms in Python.

Part 1 is Sorting Algorithms and Part 2 is Searching algorithms

Sorting Algorithms

Let us begin with a sorting algorithm. We all know that python comes with an inbuilt function to sort an array of numbers.

a = [1,2,3,4,5,6,7]
a.sort()

Python even makes it more interesting in that you can sort the numbers in a descending order.

a = [1,2,3,4,5,6,7]
a.sort(reverse = True)

However, in this article, we shall discuss other ways of sorting arrays through custom algorithms. We shall look at three algorithms.

1. Insertion Sort

This sorting algorithm involves finding the right place for each element in the array. We begin by taking the first two elements and finding the position of each relative to the other. We then add the third element and compare it to the previous elements and find its position. Other elements are added gradually to the sorted list until the list is exhausted.

#Insert sort method.
def insertion_sort(unordered_list):
#Takes in the list to be sorted as parameter.
for i in range(1,len(unordered_list)):
#We start from one since the iteration occurs upto the lenght of the list.
j = i - 1
#j is the position of the current element.
#i is the position of the next element.
next_element = unordered_list[i]
while (unordered_list[j] > next_element) and (j >=0 ):
#We swap the elements if the current element is larger that the next element.
unordered_list[j+1] = unordered_list[j]
j = j - 1
#We move to the next element.
unordered_list[j+1] = next_element
#We print out the ordered_list but its name is still unordered_list
print(unordered_list)

2. Bubble sort

This is quite similar to insertion sorting algorithm but differs in some ways. In this algorithm, we only compare adjacent elements and we then swap them if they are not in order. It is way much simpler to understand as compared to the previous algorithms.

#Bubble sort method.
def bubblesort(unordered_list):
#Takes in the list to be sorted as a parameter.
for num in range(len(unordered_list)-1,0,-1):
#We iterate over the positions in the list.
#We start with the last element and move leftwards, one step upto the first element.
for idx in range(num):
#We take a position 'idx' in the list to compare with the adjacent numbes.
if unordered_list[idx] > unordered_list[idx+1]:
#We swap the numbers if they're not in order.
temp = unordered_list[idx]
unordered_list[idx] = unordered_list[idx+1]
unordered_list[idx+1] = temp
#We print out the ordered list but retained its name which was 'unordered_lis'
print(unordered_list)

3. Selection sort

This sorting algorithm is quite different from the previous ones. Here, we get the smallest element in a list and take it to a copy list, then repeat the process for the remaining elements until all of the elements have been sorted in the list.

#Selection sort method.
def selection_sort(unordered_list):
#The function takes the list to be sorted as a parameter.
for idx in range(len(unordered_list)):
#We take the position 'idx' for the lenght of the list.
min_idx = idx
#We assume the position of the smallest element is idx hence min_idx.
for j in range(idx+1, len(unordered_list)):
if unordered_list[min_idx] > unordered_list[j]:
#We check if it is indeed the smallest number and record its position
min_idx = j
#We swap the elements so that they can be in order
unordered_list[idx], unordered_list[min_idx] = unordered_list[min_idx], unordered_list[idx]
#We print out the sorted list but we retained its name as unordered_list
print(unordered_list)

There are several ways of sorting using python algorithms. Each one of them is different from the other and they will vary with your level of skill in working with data structures. Some will be easier than others and some will run faster than others.

Let’s be friends! Follow me on Twitter and FaceBook and connect with me on LinkedIn. You can visit My website Too . Don’t forget to follow me here on Medium as well for more technophile content.

--

--

Naem Azam

Passionate self-taught Programmer, an open-source enthusiast, and maintainer. Expertise includes Programming, Linux, IT Support, Web Dev, and AI.