---
title: "Insertion Sort"
description: "Python coding for sorting numbers using insertion sort algorithm"
url: "https://www.geekboots.com/python/insertion-sort"
markdown_url: "https://www.geekboots.com/python/insertion-sort/md"
category: "Python Programming"
author: "Geekboots"
published: "2016-06-06"
updated: "2026-11-17T20:18:27.372Z"
tags: ["Python", "Python programming", "insertion sort", "sort number", "sorting", "sorting algorithm"]
---

# Insertion Sort (Python Programming)

> Python coding for sorting numbers using insertion sort algorithm

## Source Code

### insertion-sort.py

```python
#!/usr/bin/evn python

# Function for insertion sort
def insertionSort(data):
    # Loop the process within range of number list
    for i in range(1,len(data)):
        currentVal = data[i]
        pos = i
        
        # Compare upto position 'i' and swap
        while pos > 0 and data[pos-1] > currentVal:
            data[pos]=data[pos-1]
            pos = pos-1

        data[pos] = currentVal
        
numList = [5,8,1,6,3,7,2,4,9]
print('Before sort:')
print(numList)
# Calling 'insertionSort' function by passing number array
insertionSort(numList)
print('After sort:')
print(numList)


# ***** Output *****
# Before sort:
# [5, 8, 1, 6, 3, 7, 2, 4, 9]
# After sort:
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

## Related Code Examples

- [Heap Sort](https://www.geekboots.com/python/heap-sort) ([Markdown](https://www.geekboots.com/python/heap-sort/md))
- [Merge Sort](https://www.geekboots.com/python/merge-sort) ([Markdown](https://www.geekboots.com/python/merge-sort/md))
- [Quick Sort](https://www.geekboots.com/python/quick-sort) ([Markdown](https://www.geekboots.com/python/quick-sort/md))
- [Selection Sort](https://www.geekboots.com/python/selection-sort) ([Markdown](https://www.geekboots.com/python/selection-sort/md))
- [Bubble Sort](https://www.geekboots.com/python/bubble-sort) ([Markdown](https://www.geekboots.com/python/bubble-sort/md))
- [Delete file & directory](https://www.geekboots.com/python/delete-file-directory) ([Markdown](https://www.geekboots.com/python/delete-file-directory/md))
- [File Operation](https://www.geekboots.com/python/file-operation) ([Markdown](https://www.geekboots.com/python/file-operation/md))
- [Directory](https://www.geekboots.com/python/directory) ([Markdown](https://www.geekboots.com/python/directory/md))

---
Source: [Geekboots](https://www.geekboots.com/python/insertion-sort)
