# Minimum number of swaps required to sort an array

posted May 12 3 min read

## Sort in programming

Sorting data inside a computer is a common and widely used process. It means to sort a text or numbers. This sorting can be of two types, Ascendinf and descending. Ascending means from below.  A to Z and 0 to 9 at the top, while descent means Z to A and 9 to 0 at the top.  To understand the method of sorting, we use a list of numbers as an example.Popular sorting algorithms.

Simple sorts

Insertion sort

Selection sort

Efficient sorts

Merge sort

Heapsort

Quicksort

Shellsort

Counting sort

Bucket sort

Comb sort

Bubble sort

Example:-

``````Find minimum number of swap

Input: arr[] = { 7, 1, 3, 2, 4, 5, 6 }
O/P: 5

Explanation:
i           arr             swap (indices)
0   [7, 1, 3, 2, 4, 5, 6]   swap (0, 3)
1   [2, 1, 3, 7, 4, 5, 6]   swap (0, 1)
2   [1, 2, 3, 7, 4, 5, 6]   swap (3, 4)
3   [1, 2, 3, 4, 7, 5, 6]   swap (4, 5)
4   [1, 2, 3, 4, 5, 7, 6]   swap (5, 6)
5   [1, 2, 3, 4, 5, 6, 7]
Therefore, total number of swaps = 5

Input: arr[] = { 2, 3, 4, 1, 5 }
``````

Output

``3``

So there are many ways to sort the lists. But the simplest of them is Exchange sort.  Two items are competed against each other. If they meet the condition of the given order then no action is taken on them and if they do not meet this condition then their mutual place is changed.  The number of marks decreases in the next stage. I hope you have understood sorting. Let's do some practicals and try to understand it more.  Array is used as an argument inside a function.