
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find out the number of shifts required to sort an array using insertion sort in python
Suppose we are given an array and asked to perform insertion sort on it. In insertion sort, each element in an array is shifted to its correct position in the array. We have to find out the total number of shifts required to sort an array. The total number of shifts is an integer number and if the array is already sorted, we return 0.
So, if the input is like input_array = [4, 5, 3, 1, 2], then the output will be 8
[4, 5, 3, 1, 2] = 0 shifts [4, 5, 3, 1, 2] = 0 shifts [3, 4, 5, 1, 2] = 2 shifts [1, 3, 4, 5, 2] = 3 shifts [1, 2, 3, 4, 5] = 3 shifts
total number of shifts are = 0 + 0 + 2 + 3 + 3 = 8.
To solve this, we will follow these steps −
- length := size of input_arr
- temp_arr := a new list of size 1000001 initialized with 0s
- ans := 0
- for each item in input_arr, do
- val := item
- while val > 0, do
- ans := ans + temp_arr[val]
- val := val - (val AND -val)
- val := item
- while val <= 1000000, do
- temp_arr[val] := temp_arr[val] + 1
- val := val + (val AND -val)
- ans := length * (floor value of (length - 1) / 2) - ans
- return ans
Example
Let us see the following implementation to get better understanding −
def solve(input_arr): length = len(input_arr) temp_arr = [0] * 1000001 ans = 0 for item in input_arr: val = item while val > 0: ans += temp_arr[val] val -= val & -val val = item while val <= 1000000: temp_arr[val] = temp_arr[val] + 1 val += val & -val ans = length * (length - 1) // 2 - ans return ans print(solve([4, 5, 3, 1, 2]))
Input
[4, 5, 3, 1, 2]
Output
8
- Related Articles
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Golang Program to sort an array in descending order using insertion sort
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Program to find number of swaps required to sort the sequence in python
- Insertion Sort in Python Program
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- Minimum number of swaps required to sort an array in C++
- Python Program for Insertion Sort
- C program to sort an array by using merge sort
- Python Program for Binary Insertion Sort
- Python Program for Recursive Insertion Sort
- Java program to implement insertion sort
- C++ Program to Implement Insertion Sort
- Write a Golang program to sort an array using Bubble Sort
- C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm

Advertisements