Skip to content

Commit

Permalink
README File added
Browse files Browse the repository at this point in the history
  • Loading branch information
admin authored and admin committed Jan 21, 2024
1 parent 2a52aa4 commit 6a3f39f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/sorting/python/insertion_sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Insertion Sort

## Overview
This Python module provides an implementation of the insertion sort algorithm. The `insertionsort_v1` function takes an input array and sorts it in ascending order using the insertion sort algorithm.

## Installation
No installation is required for this module. Simply include the `insertionsort.py` file in your project.

## Usage

```python
from insertionsort import insertionsort_v1


# Example
arr = [4, 2, 1, 3, 5]
sorted_arr = insertionsort_v1(arr)
print(sorted_arr) # Output: [1, 2, 3, 4, 5]
24 changes: 23 additions & 1 deletion src/sorting/python/insertion_sort/src/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
def insertionsort_v1(arr):
"""
Sorts the input array using the insertion sort algorithm.
Args:
arr (List[int]): The input array to be sorted.
Returns:
List[int]: The sorted array in ascending order.
"""

for i in range(1, len(arr)):
# Select the current element to be inserted at the correct position
key = arr[i]
j = i - 1
# Initialize the index of the previous element
j = i - 1

# Move elements greater than the key to the right
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Shift the element to the right
arr[j + 1] = arr[j]
# Move to the previous element
j -= 1

return arr
# Insert the key at the correct position
arr[j + 1] = key

return arr
# Return the sorted array
return arr

0 comments on commit 6a3f39f

Please sign in to comment.