swap sort Algorithm
The Swap Sort Algorithm, also known as the Bubble Sort Algorithm, is a simple and intuitive sorting algorithm used to arrange a list of elements in a specific order, either ascending or descending. The algorithm works by repeatedly iterating through the list, comparing each pair of adjacent elements, and swapping them if they are in the incorrect order. This process continues until the entire list is sorted, with the smaller elements "bubbling" to the front of the list and the larger elements "sinking" to the end of the list. Due to its simplicity, the algorithm is commonly used as an introductory example for teaching sorting techniques, but it is not efficient for large data sets.
In a more detailed explanation, the Swap Sort Algorithm starts at the beginning of the list and compares the first two elements. If the elements are in the wrong order, they are swapped; otherwise, they remain in their positions. The algorithm then moves to the next pair of elements and repeats the comparison and swapping process. This continues until the end of the list is reached. After the first iteration, the largest element will be in its correct position at the end of the list. The algorithm then iterates through the list again, but this time stops one element before the last sorted element. This process is repeated until the entire list is sorted, with each iteration placing the next largest element in its correct position. The algorithm has a time complexity of O(n^2) in the worst and average cases, making it inefficient for large data sets. However, it can be easily adapted to become more efficient under certain conditions, such as when the list is already partially sorted.
// C++ program to find minimum number of swaps required to sort an array
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
// Function returns the minimum number of swaps
// required to sort the array
int minSwaps(int arr[], int n) {
// Create an array of pairs where first
// element is array element and second element
// is position of first element
std::pair<int, int> arrPos[n];
for (int i = 0; i < n; i++) {
arrPos[i].first = arr[i];
arrPos[i].second = i;
}
// Sort the array by array element values to
// get right position of every element as second
// element of pair.
std::sort(arrPos, arrPos + n);
// To keep track of visited elements. Initialize
// all elements as not visited or false.
std::vector<bool> vis(n, false);
// Initialize result
int ans = 0;
// Traverse array elements
for (int i = 0; i < n; i++) {
// already swapped and corrected or
// already present at correct pos
if (vis[i] || arrPos[i].second == i)
continue;
// find out the number of node in
// this cycle and add in ans
int cycle_size = 0;
int j = i;
while (!vis[j]) {
vis[j] = 1;
// move to next node
j = arrPos[j].second;
cycle_size++;
}
// Update answer by adding current cycle.
if (cycle_size > 0) {
ans += (cycle_size - 1);
}
}
// Return result
return ans;
}
// program to test
int main() {
int arr[] = {6, 7, 8, 1, 2, 3, 9, 12};
int n = (sizeof(arr) / sizeof(int));
std::cout << minSwaps(arr, n);
return 0;
}