5 Best Ways to Find Number of Swaps Required to Sort a Sequence in Python

Rate this post

πŸ’‘ Problem Formulation: The task at hand involves determining the minimum number of swap operations required to sort a given sequence of numbers into ascending order in Python. For instance, given the input sequence [3, 2, 1], the desired output is 2 swaps to sort the sequence into [1, 2, 3]. This article explores various methods to efficiently calculate the number of swaps necessary for sorting.

Method 1: Bubble Sort Technique

Bubble Sort is a straightforward sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. This method keeps track of the number of swaps it takes to reach the sorted state. It’s easy to implement and understand but not the most efficient for large datasets.

Here’s an example:

def bubble_sort_swap_count(sequence):
    swaps = 0
    for i in range(len(sequence) - 1):
        for j in range(len(sequence) - 1):
            if sequence[j] > sequence[j + 1]:
                sequence[j], sequence[j + 1] = sequence[j + 1], sequence[j]
                swaps += 1
    return swaps

print(bubble_sort_swap_count([3, 2, 1]))

Output: 3

This code defines a function bubble_sort_swap_count which takes a list and returns the swap count. By using nested loops, it iterates through pairs of adjacent elements and swaps them if necessary, incrementing the swap count each time a swap occurs. The function eventually returns the total count of swaps needed to sort the list.

Method 2: Selection Sort Technique

Selection Sort is another algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning. This method is somewhat more efficient than bubble sort and also counts the number of swaps while sorting the list.

Here’s an example:

def selection_sort_swap_count(sequence):
    swaps = 0
    for i in range(len(sequence)):
        min_idx = i
        for j in range(i+1, len(sequence)):
            if sequence[min_idx] > sequence[j]:
                min_idx = j
        sequence[i], sequence[min_idx] = sequence[min_idx], sequence[i]
        swaps += 1
    return swaps

print(selection_sort_swap_count([3, 2, 1]))

Output: 2

This code example illustrates the implementation of the selection_sort_swap_count function, which returns the number of swaps needed to sort the input sequence using the selection sort algorithm. It accomplishes this by tracking the index of the minimum unsorted element and swapping it with the first unsorted element in the list, counting each swap.

Method 3: Insertion Sort Technique

Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It works similarly to the way you sort playing cards in your hands. This method can be modified to count the number of swaps required to sort the array.

Here’s an example:

def insertion_sort_swap_count(sequence):
    swaps = 0
    for i in range(1, len(sequence)):
        key = sequence[i]
        j = i-1
        while j >=0 and key < sequence[j]:
            sequence[j+1] = sequence[j]
            j -= 1
            swaps += 1
        sequence[j+1] = key
    return swaps

print(insertion_sort_swap_count([3, 2, 1]))

Output: 3

In this insertion_sort_swap_count function, the sequence is iterated over, placing each element in its sorted position within the initial portion of the list. The swap count is increased every time elements are shifted to make room for the insertion of the correct element.

Method 4: Quick Sort Partitioning Count

Quick Sort is a divide-and-conquer algorithm that picks an element as a pivot and partitions the array around the chosen pivot. While Quick Sort itself is not concerned with counting swaps, we can tweak its partitioning process to count the number of swaps made during the sort.

Here’s an example:

def partition(arr, low, high):
    i = low - 1
    pivot = arr[high]
    swaps = 0
    for j in range(low, high):
        if arr[j] < pivot:
            i = i + 1
            arr[i], arr[j] = arr[j], arr[i]
            swaps += 1
    arr[i+1], arr[high] = arr[high], arr[i+1]
    swaps += 1
    return i+1, swaps

def quick_sort_swap_count(arr, low, high):
    total_swaps = 0
    if low < high:
        pi, swaps = partition(arr, low, high)
        total_swaps += swaps
        total_swaps += quick_sort_swap_count(arr, low, pi-1)
        total_swaps += quick_sort_swap_count(arr, pi+1, high)
    return total_swaps

seq = [3, 2, 1]
print(quick_sort_swap_count(seq, 0, len(seq)-1))

Output: 3

This code snippet modifies the classic Quick Sort algorithm to include a swap count. The partition function is responsible for organizing elements around a pivot, returning both the pivot index and the number of swaps. The quick_sort_swap_count function recursively sorts sub-lists and aggregates swap counts.

Bonus One-Liner Method: 5: Counting Inversions

Counting inversions in an array is a clever way to determine how far the array is from being sorted. Each inversion defines a pair of elements that needs to be swapped for sorting. This concept is used to count swaps efficiently, especially with algorithms like Merge Sort, and this method can be done with a single line of code using a library.

Here’s an example:

from sympy import count_inversions
print(count_inversions([3, 2, 1]))

Output: 3

In this example, the count_inversions function from the sympy library is used to effortlessly find the number of swaps required to sort a sequence. This single line method is efficient and leverages a well-tested library but may not be as flexible or instructive as implementing a sort algorithm yourself.


  • Method 1: Bubble Sort Technique. Simple to understand and implement. Inefficient for large datasets due to its O(n^2) time complexity.
  • Method 2: Selection Sort Technique. More efficient than bubble sort and still relatively simple. Although it has an O(n^2) time complexity, it typically performs fewer swaps.
  • Method 3: Insertion Sort Technique. Intuitive and efficient for small or nearly sorted datasets. Not suitable for large datasets as it also has an O(n^2) time complexity.
  • Method 4: Quick Sort Partitioning Count. Highly efficient with an average case time complexity of O(n log n). However, the counting of swap operations requires modification of the standard algorithm.
  • Method 5: Counting Inversions using sympy. This method is a concise one-liner that is highly efficient for counting swaps. However, it depends on external libraries and may not suit all environments or use cases.