Evaluating Performance on Classic Sorting Algorithms in Python and Rust

Evaluating Performance on Classic Sorting Algorithms in Python and Rust

Which will emerge fastest in sorting? Python or Rust? 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.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Evaluating Performance on Search Algorithms in Python and Rust

Speed in search algo in Python and Rust? Ever wondered if the search algorithms that you have learnt in high school or college/university, would perform differently in different programming languages?

Guide to Python Programming Language

Guide to Python Programming Language

Intersection of Two Sets of Coordinates, Sort Colors Using Python OOP

Solving Two programming problems, one with a sorting algorithm and another with OOP in Python.

Visualize an Interesting Sorting Algorithms With Python

There are various types of sorting algorithms out there and sometimes it becomes very difficult to understand their internal working without visualization. Hence I decided to visualize these sorting algorithms in python with the help of matplotlib.animations library.

Quick Sort: The most performant sorting algorithm

Get to know the most efficient sorting algorithm. Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array an element called a pivot is selected. All elements smaller ...