Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Insertion Sorting using while in python

def insertion_sort(l: list):
  """sorts the input list using the insertion sort algorithm"""
  index = 1
  while index < len(l):
    insertion_index = index
    while insertion_index > 0 and l[insertion_index] < l[insertion_index - 1]:
      # swap the two elements
      element = l[insertion_index]
      l[insertion_index] = l[insertion_index - 1]
      l[insertion_index - 1] = element
      insertion_index = insertion_index - 1
    index = index + 1
Source by cs.brown.edu #
 
PREVIOUS NEXT
Tagged: #Insertion #Sorting #python
ADD COMMENT
Topic
Name
4+1 =