

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- 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
- Minimum number of swaps required to sort an array in C++
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find how many lines intersect in Python
- Program to find out the number of shifts required to sort an array using insertion sort in python
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum swaps required to make given anagram in python
- Python Program to find out how many cubes are cut
- C++ program to count at least how many operations needed for given conditions
- C program to sort an array by using merge sort
- Program to find minimum swaps to arrange a binary grid using Python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
Advertisements