Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Insertion Sort

Aim

The main aim of the script is to sort numbers in list using insertion sort.

Purpose

The main purpose is to sort list of any numbers in O(n) or O(n^2) time complexity.

Short description of package/script

Takes in an array.
Sorts the array and prints sorted array along with the number of swaps and comparisions made. Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

Detailed explanation of script, if needed

To sort an array of size n in ascending order:
1: Iterate from a[1] to a[n] over the array.
2: Compare the current element (val) to its predecessor.
3: If the val is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Setup instructions

Download code and run it in any python editor. Latest version is always better.

Compilation Steps

1.Edit array a and enter your array/list you want to sort. 2. Run the code

Sample Test Cases

Test case 1:

input:
a = [34,5,77,33]

output :

5, 33, 34, 77 along with
no. of swaps = 3
no. of comparisons=5

Test case 2

input
a=[90,8,11,3,2000,700,478]

Output:

No. of swaps= 8
No. of comparisions=12
Sorted Array is:
3 8 11 90 478 700 2000

Test case 3

input
a=[0,33,7000,344,-88,2000]

output:

No. of swaps= 6
No. of comparisions=10
Sorted Array is:
-88 0 33 344 2000 7000

Output

Author(s)

Ritika Chand