• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Data Dependence

Data Dependence

Learn about software development. Covering topics such as Coding in Python and JavaScript, developer productivity and more.

  • Home
  • Top Resources
  • Who Are You?
  • Show Search
Hide Search
You are here: Home / Programming / [Snippet] Insertion Sort Python Implementation

[Snippet] Insertion Sort Python Implementation

· · Leave a Comment

Recently I’ve been reviewing basic algorithms and data structures. I have one more year left of university and then I have to find a job. You hear all kind of crazy stories from programming interviews, but one recurring theme is algorithms and data structures. Long story, short; know them.
So, in this post we’ll take a look at insertion sort. One of the most basic sorting algorithms you’ll learn.

Insertion sort is a sorting algorithm that has a worst case of $O(n^2)$. It is one of the basic sorting algorithms that you would learn on a Computer Science course. Wikipedia has a good explanation of it.

This is an implementation of it that I wrote when following along to the CLRS book.

def insertion_sort(array):
    for j, val in enumerate(array):
        key = array[j]
        i = j - 1
        while i >= 0 and array[i] > key:
            array[i + 1] = array[i]
            i = i - 1
        array[i + 1] = key

I’ll be posting more implementations of basic algorithms and data structures in the future, so if you find this kind of post helpful, by all means subscribe to my posts feed and learn as I learn.

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list and follow me on Twitter so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.

I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.

I’m friendly, so feel free to say hello!

Twitter is the best place for a chat.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)

Related

Programming insertion sort, python, snippet

Primary Sidebar

LEVEL UP with the Data Dependence Newsletter! Subscribe!


Top Posts & Pages

  • How to Find Unclosed Tags and Brackets Using a Stack
  • How to Build a GUI in Python 3.5
  • Mocking in Python - How to Bypass Expensive and External Code Within Your Tests
  • How to Logically Group Your Python Code into Modules
  • A Quick Guide to Slicing in Python - Become a Python Ninja

Data Dependence

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

  • Home
  • Top Resources
  • Who Are You?