The main aim of the script is to sort numbers in list using insertion sort.
The main purpose is to sort list of any numbers in O(n) or O(n^2) time complexity.
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.
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.
Download code and run it in any python editor. Latest version is always better.
1.Edit array a and enter your array/list you want to sort. 2. Run the code
input:
a = [34,5,77,33]
output :
5, 33, 34, 77 along with
no. of swaps = 3
no. of comparisons=5
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
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
