Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertionSort.py #525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Python/sorting_Algorithms/Insertion_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def insertionSort(list):
# all the values after the first
index_length = range(1, len(list))
# to do an operation on all these values
# for all the value is the index_length value,
for i in index_length:
# we want to sort those values
sort = list[i]
# while the item to the left is greater than the item
# to the right
# notice that we also have to write i > 0 bc python allows
# for negative indexing
while list[i-1] > sort and i > 0:
# swap
list[i], list[i-1] = list[i-1], list[i]
# to continue doing comparisons down the list,
# look at the next item
i = i - 1

return list

print(insertionSort([7, 3, 4, 1, 9]))

'''
THIS IS HOW IT WORKS
# Mark the first element, 10, as sorted
[10, 3, 2, 6]
# Look at next element, 3, 3 < 10
[10, 3, 2, 6]
# Extract 3 and move it before 10, mark first element, 3 as sorted
[3, 10, 2, 6]
# Look at the next unsorted element, 2, 2 < 10, 2 < 3
[3, 10, 2, 6]
# Extract 2 and move it before 3, mark first element, 2, as sorted
[2, 3, 10, 6]
# Look at the next unsorted element, 6, 6 < 10, 6 > 3
[2, 3, 10, 6]
# Extract 6 and move it before 10
[2, 3, 6, 10]
'''