Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions Sorting/InsertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This is the Python version of Insertion Sort
# Code is contributed by: Italo Vinicius.
#
# Insertion sort is an Sorting Algorithm which takes compelxity: O(n²)

# Function to do insertion sort
def insertionSort(arr):

# Traverse through 1 to len(arr)
for i in range(1, len(arr)):

key = arr[i]

# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

if __name__ == "__main__":
arr = list(map(int,input().split()))
insertionSort(arr)

print(' '.join(str(x) for x in arr))
27 changes: 27 additions & 0 deletions Sorting/SelectionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This is the Python version of Selection Sort
# Code is contributed by: Italo Vinicius.
#
# Selection sort is an Sorting Algorithm which takes compelxity: O(n²)

# Function to do selection sort
def selectionSort(arr):

# Traverse through all array elements
for i in range(len(arr)):

# Find the minimum element in remaining
# unsorted array
min_idx = i
for j in range(i+1, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j

# Swap the found minimum element with
# the first element
arr[i], arr[min_idx] = arr[min_idx], arr[i]

if __name__ == "__main__":
arr = list(map(int,input().split()))
selectionSort(arr)

print(' '.join(str(x) for x in arr))