It sorts lists using three different techniques

Learnt how to sort lists using Selection Sort, Bubble Sort and Insertion Sort

def selectionsort(arr):
  length = len(arr)
  for i in range(0,length):
    min_idx = i 
    for j in range(i+1, length): 
        if arr[min_idx] > arr[j]: 
            min_idx = j 

    arr[i], arr[min_idx] = arr[min_idx], arr[i]

  print("Sorted using selection sort",arr)

def bubblesort(arr):
  length = len(arr)
  for i in range(0,length): 
        for j in range(0, length-i-1): 
            if arr[j] > arr[j+1] : 
                arr[j], arr[j+1] = arr[j+1], arr[j] 

  print("Sorted using bubble sort",arr)

def insertionsort(arr):
  length = len(arr)
  for i in range(1, length): 

        key = arr[i] 
        j = i-1
        while j >=0 and key < arr[j] : 
                arr[j+1] = arr[j] 
                j -= 1
        arr[j+1] = key 

  print("Sorted using insertion sort",arr)


print("Enter Array in one line seperating each element with a single space")
array = list(map(int, input().split()))
print("The array inputed is:",array)
selectionsort(array)
bubblesort(array)
insertionsort(array)

Built With

Share this project:

Updates