
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find how many swaps needed to sort an array in Python
Suppose, we have an array called nums and we have to find the number of swaps needed to make nums sorted in any order, either ascending or descending.
So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2,5,4,3,6]. Then, if we swap the numbers 5 and 3, the array will be [2,3,4,5,6]. So 2 swaps are needed to make the array sorted in ascending order.
To solve this, we will follow these steps −
- Define a function swap_count() . This will take input_arr
- pos := new list containing tuples (item_postion, item) for each item in input_arr
- sort the list pos according to the items in input_arr
- cnt := 0
- for index in range 0 to size of input_arr, do
- while True, do
- if pos[index, 0] is same as index, then
- exit from the loop
- otherwise,
- cnt := swap_count + 1
- swap_index := pos[index, 0]
- swap the values of (pos[index], pos[swap_index])
- if pos[index, 0] is same as index, then
- while True, do
- return cnt
- From the main function/method, do the following −
- return minimum of (swap_count(input_arr) , swap_count(input_arr in reverse order))
Example
Let us see the following implementation to get better understanding −
def swap_count(input_arr): pos = sorted(list(enumerate(input_arr)), key=lambda x: x[1]) cnt = 0 for index in range(len(input_arr)): while True: if (pos[index][0] == index): break else: cnt += 1 swap_index = pos[index][0] pos[index], pos[swap_index] = pos[swap_index], pos[index] return cnt def solve(input_arr): return min(swap_count(input_arr), swap_count(input_arr[::-1])) nums = [2, 5, 6, 3, 4] print(solve(nums))
Input
[2, 5, 6, 3, 4]
Output
2
- Related Articles
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find number of swaps required to sort the sequence in python
- Minimum number of swaps required to sort an array in C++
- C++ program to find minimum how many operations needed to make number 0
- C++ program to find minimum how many coins needed to buy binary string
- Program to find out the number of shifts required to sort an array using insertion sort in python
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Golang Program To Sort An Array
- Program to find minimum swaps required to make given anagram in python
- Program to find expected number of shuffle required to sort the elements of an array in Python
- Program to sort an array based on the parity values in Python
- Python program to sort the elements of an array in ascending order
- Python program to sort the elements of an array in descending order
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Program to find how many lines intersect in Python

Advertisements