-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insertion sort added
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
This is a pure Python implementation of the insertion sort algorithm | ||
""" | ||
|
||
""" Implementation of the insertion sort algorithm in Python | ||
:parameter array: A mutable ordered collection with comparable items inside | ||
:return : the same collection ordered by ascending | ||
Input | ||
Enter the size of list: | ||
6 | ||
Enter the numbers: | ||
5 | ||
0 | ||
2 | ||
1 | ||
4 | ||
3 | ||
Output | ||
After Insertion sort: | ||
0 1 2 3 4 5 | ||
""" | ||
def insertion_sort(array): | ||
# Loop from the second element of the array until | ||
# the last element | ||
for i in range(1, len(array)): | ||
# This is the element we want to position in its | ||
# correct place | ||
key_item = array[i] | ||
|
||
# Initialize the variable that will be used to | ||
# find the correct position of the element referenced | ||
# by `key_item` | ||
j = i - 1 | ||
|
||
# Run through the list of items (the left | ||
# portion of the array) and find the correct position | ||
# of the element referenced by `key_item`. Do this only | ||
# if `key_item` is smaller than its adjacent values. | ||
while j >= 0 and array[j] > key_item: | ||
# Shift the value one position to the left | ||
# and reposition j to point to the next element | ||
# (from right to left) | ||
array[j + 1] = array[j] | ||
j -= 1 | ||
|
||
# When you finish shifting the elements, you can position | ||
# `key_item` in its correct location | ||
array[j + 1] = key_item | ||
|
||
return array | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
|
||
|
||
user_input = [] | ||
n = int(input("Enter size of array: ")) | ||
print("Enter array elements:\n") | ||
for i in range(0, n): | ||
user_input.append(int(input())) | ||
|
||
# calling the insertion_sort function | ||
sorted_collection = insertion_sort(user_input) | ||
print("After insertion sort") | ||
print(*sorted_collection, sep=" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Insertion Sort | ||
## Introduction | ||
|
||
<p> | ||
A simple sorting algorithm which works on the idea of picking elements from the unsorted part one by one and inserting them into their right positions in the sorted part. | ||
The array is virtually divided into 2 parts, a sorted one and an unsorted one. | ||
Just like we arrange a group of shuffled pages according to their page numbers, values from unsorted parts are picked and placed at the right positions in the sorted portion. | ||
</p> | ||
|
||
|
||
![image](https://user-images.githubusercontent.com/74819092/119613948-538bd700-be1b-11eb-8351-dec8333cb436.png) | ||
|
||
* As we can see above, we start from the first 2 elements on the left. Comparing them we know that 3 < 4 so we need to position 3 before 4, so we swap their positions. <br> | ||
* Next, 2 becomes the key element which we compare with all elements before it until we find something smaller than 2. Since 2 is smaller than both 3 and 4, we place it at the beginning of the array which is its correct position. | ||
Since 12 is already larger than any element before it, its position is correct (for the current iteration).<br> | ||
* Finally, comparing 9 with its predecessors, we see it's only smaller than 12 so we swap the positions of 9 and 12 to get the fully sorted array.<br> | ||
## Algorithm | ||
For sorting an array of size n in ascending order: | ||
|
||
* Start your loop from ```arr[1]``` till ```arr[n]``` | ||
* Compare the selected element of the current iteration (key) to the element before it. | ||
* If this key is smaller than its predecessor, compare it to the elements before the predecessor. | ||
* Shift all the greater elements up by one position to make space for the smaller element. | ||
|
||
# Complexity Analysis | ||
n is the number of elements in the array.<br> | ||
|
||
## Time Complexity: | ||
|
||
Best Case: O (n)<br> | ||
Average Case: O (n2)<br> | ||
Worst Case: O (n2)<br> | ||
Space Complexity: O (1)<br> |