📜  使用插入排序对数组进行排序所需的计数交换

📅  最后修改于: 2021-04-28 14:15:06             🧑  作者: Mango

给定大小为N ( 1≤N≤10 5 )的数组A [] ,任务是计算使用插入排序算法对数组进行排序所需的交换次数。

例子:

方法:可以使用分而治之算法(合并排序)解决问题。请按照以下步骤解决问题:

  • 将数组分为两半,然后递归遍历这两个两半。
  • 对每一半进行排序并计算所需的交换次数。
  • 最后,打印所需的交换总数。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Stores the sorted
// array elements
int temp[100000];
 
// Function to count the number of
// swaps required to merge two sorted
// subarray in a sorted form
long int merge(int A[], int left,
               int mid, int right)
{
 
    // Stores the count of swaps
    long int swaps = 0;
 
    int i = left, j = mid, k = left;
 
    while (i < mid && j <= right) {
 
        if (A[i] <= A[j]) {
            temp[k] = A[i];
            k++, i++;
        }
        else {
            temp[k] = A[j];
            k++, j++;
            swaps += mid - i;
        }
    }
    while (i < mid) {
        temp[k] = A[i];
        k++, i++;
    }
 
    while (j <= right) {
        temp[k] = A[j];
        k++, j++;
    }
 
    while (left <= right) {
        A[left] = temp[left];
        left++;
    }
 
    return swaps;
}
 
// Function to count the total number
// of swaps required to sort the array
long int mergeInsertionSwap(int A[],
                            int left, int right)
{
    // Stores the total count
    // of swaps required
    long int swaps = 0;
    if (left < right) {
 
        // Find the middle index
        // splitting the two halves
        int mid = left + (right - left) / 2;
 
        // Count the number of swaps
        // required to sort the left subarray
        swaps += mergeInsertionSwap(A, left, mid);
 
        // Count the number of swaps
        // required to sort the right subarray
        swaps += mergeInsertionSwap(A, mid + 1, right);
 
        // Count the number of swaps required
        // to sort the two sorted subarrays
        swaps += merge(A, left, mid + 1, right);
    }
    return swaps;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 1, 3, 1, 2 };
    int N = sizeof(A) / sizeof(A[0]);
    cout << mergeInsertionSwap(A, 0, N - 1);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Stores the sorted
  // array elements
  static int temp[] = new int[100000];
 
  // Function to count the number of
  // swaps required to merge two sorted
  // subarray in a sorted form
  static int merge(int A[], int left,
                   int mid, int right)
  {
 
    // Stores the count of swaps
    int swaps = 0;
    int i = left, j = mid, k = left;
    while (i < mid && j <= right)
    {
      if (A[i] <= A[j])
      {
        temp[k] = A[i];
        k++; i++;
      }
      else
      {
        temp[k] = A[j];
        k++; j++;
        swaps += mid - i;
      }
    }
    while (i < mid)
    {
      temp[k] = A[i];
      k++; i++;
    }
 
    while (j <= right)
    {
      temp[k] = A[j];
      k++; j++;
    }
 
    while (left <= right)
    {
      A[left] = temp[left];
      left++;
    }
    return swaps;
  }
 
  // Function to count the total number
  // of swaps required to sort the array
  static int mergeInsertionSwap(int A[],
                                int left, int right)
  {
    // Stores the total count
    // of swaps required
    int swaps = 0;
    if (left < right)
    {
 
      // Find the middle index
      // splitting the two halves
      int mid = left + (right - left) / 2;
 
      // Count the number of swaps
      // required to sort the left subarray
      swaps += mergeInsertionSwap(A, left, mid);
 
      // Count the number of swaps
      // required to sort the right subarray
      swaps += mergeInsertionSwap(A, mid + 1, right);
 
      // Count the number of swaps required
      // to sort the two sorted subarrays
      swaps += merge(A, left, mid + 1, right);
    }
    return swaps;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int A[] = { 2, 1, 3, 1, 2 };
    int N = A.length;
    System.out.println(mergeInsertionSwap(A, 0, N - 1));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python3 program to implement
# the above approach
 
# Stores the sorted
# array elements
temp = [0] * 100000
 
# Function to count the number of
# swaps required to merge two sorted
# subarray in a sorted form
def merge(A, left, mid, right):
     
    # Stores the count of swaps
    swaps = 0
 
    i, j, k = left, mid, left
     
    while (i < mid and j <= right):
         
        if (A[i] <= A[j]):
            temp[k] = A[i]
            k, i = k + 1, i + 1
        else:
            temp[k] = A[j]
            k, j = k + 1, j + 1
            swaps += mid - i
 
    while (i < mid):
        temp[k] = A[i]
        k, i = k + 1, i + 1
 
    while (j <= right):
        temp[k] = A[j]
        k, j = k + 1, j + 1
 
    while (left <= right):
        A[left] = temp[left]
        left += 1
 
    return swaps
 
# Function to count the total number
# of swaps required to sort the array
def mergeInsertionSwap(A, left, right):
     
    # Stores the total count
    # of swaps required
    swaps = 0
     
    if (left < right):
 
        # Find the middle index
        # splitting the two halves
        mid = left + (right - left) // 2
 
        # Count the number of swaps
        # required to sort the left subarray
        swaps += mergeInsertionSwap(A, left, mid)
 
        # Count the number of swaps
        # required to sort the right subarray
        swaps += mergeInsertionSwap(A, mid + 1, right)
 
        # Count the number of swaps required
        # to sort the two sorted subarrays
        swaps += merge(A, left, mid + 1, right)
 
    return swaps
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 2, 1, 3, 1, 2 ]
    N = len(A)
     
    print (mergeInsertionSwap(A, 0, N - 1))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
 // Stores the sorted
  // array elements
  static int[] temp = new int[100000];
 
  // Function to count the number of
  // swaps required to merge two sorted
  // subarray in a sorted form
  static int merge(int[] A, int left,
                   int mid, int right)
  {
 
    // Stores the count of swaps
    int swaps = 0;
    int i = left, j = mid, k = left;
    while (i < mid && j <= right)
    {
      if (A[i] <= A[j])
      {
        temp[k] = A[i];
        k++; i++;
      }
      else
      {
        temp[k] = A[j];
        k++; j++;
        swaps += mid - i;
      }
    }
    while (i < mid)
    {
      temp[k] = A[i];
      k++; i++;
    }
 
    while (j <= right)
    {
      temp[k] = A[j];
      k++; j++;
    }
 
    while (left <= right)
    {
      A[left] = temp[left];
      left++;
    }
    return swaps;
  }
 
  // Function to count the total number
  // of swaps required to sort the array
  static int mergeInsertionSwap(int[] A,
                                int left, int right)
  {
     
    // Stores the total count
    // of swaps required
    int swaps = 0;
    if (left < right)
    {
 
      // Find the middle index
      // splitting the two halves
      int mid = left + (right - left) / 2;
 
      // Count the number of swaps
      // required to sort the left subarray
      swaps += mergeInsertionSwap(A, left, mid);
 
      // Count the number of swaps
      // required to sort the right subarray
      swaps += mergeInsertionSwap(A, mid + 1, right);
 
      // Count the number of swaps required
      // to sort the two sorted subarrays
      swaps += merge(A, left, mid + 1, right);
    }
    return swaps;
  }
 
  // Driver Code
  static public void Main()
  {
    int[] A = { 2, 1, 3, 1, 2 };
    int N = A.Length;
    Console.WriteLine(mergeInsertionSwap(A, 0, N - 1));
  }
}
 
// This code is contributed by code_hunt.


输出:
4

时间复杂度: O(N * log(N))
辅助空间: O(N)