Backstory

Algorithm performance has always been one of the key factors in solving complex real world use cases. We need efficient algorithms especially in the case of sorting huge amounts of data. In this article, I will walk you through how Bubble, Insertion, Quick and Merge sort algorithms are implemented in Python and Rust programming languages to find out if the programming language does matter in getting the edge in performance.

Prerequisites

Before continuing with this article, you should have,

  • basic programming experience in Python and Rust and
  • basic understanding of some classic sorting algorithms

Environment

  • Docker version 19.03.6
  • Python 3.7.3
  • Rust 1.43.1

Bubble Sort

Python

Source Code:

import random
import datetime

def BubbleSortAsc(array):
 no_swap = True

 while no_swap:
  no_swap = False
  for position in range(0, len(array)-1):
   if array[position] > array[position+1]:
    ## swap
    temp = array[position]
    array[position] = array[position+1]
    array[position+1] = temp
    no_swap = True

To Run:

if __name__ == "__main__":
 array = [index for index in range(1, 100001)]
 random.shuffle(array)
 start = datetime.datetime.now()
 BubbleSortAsc(array)
 end = datetime.datetime.now()
 print("It took: ", (end-start).total_seconds()*1000, "ms to sort")

Performance:

  • It took 9953.896999999999 milliseconds to sort random **10000 **positive integers in ascending order
  • Time takes too long to sort random **100000 **positive integers in ascending order. I wasn’t patient enough.

#sorting-algorithms #python-programming #evaluating-technology #performance #rust-programming

Evaluating Performance on Classic Sorting Algorithms in Python and Rust
1.20 GEEK