Maison  >  Article  >  développement back-end  >  Programme C/C++ écrit à l'aide de l'algorithme de tri par fusion pour calculer les nombres inversés dans un tableau

Programme C/C++ écrit à l'aide de l'algorithme de tri par fusion pour calculer les nombres inversés dans un tableau

PHPz
PHPzavant
2023-08-25 19:33:28995parcourir

Programme C/C++ écrit à laide de lalgorithme de tri par fusion pour calculer les nombres inversés dans un tableau

La représentation inversée d'un tableau ; combien de modifications sont nécessaires pour convertir le tableau dans sa forme triée. Lorsque le tableau est déjà trié, 0 inversion est nécessaire, tandis que dans les autres cas, si le tableau est inversé, le nombre maximum d'inversions est atteint.

Pour résoudre ce problème, nous suivrons la méthode de tri par fusion pour réduire la complexité temporelle et utiliserons l'algorithme diviser pour régner.

Input

A sequence of numbers. (1, 5, 6, 4, 20).

Output

Le nombre d'inversions nécessaires pour trier les nombres par ordre croissant.

Here the number of inversions are 2.
First inversion: (1, 5, 4, 6, 20)
Second inversion: (1, 4, 5, 6, 20)

Algorithm

merge(array, tempArray, left, mid, right)

Input - deux tableaux qui ont été fusionnés, index gauche, droit et central.

Output - tableaux fusionnés dans l'ordre trié.

Begin
   i := left, j := mid, k := right
   count := 0
   while i <= mid -1 and j <= right, do
      if array[i] <= array[j], then
         tempArray[k] := array[i]
         increase i and k by 1
      else
         tempArray[k] := array[j]
         increase j and k by 1
         count := count + (mid - i)
   done
   while left part of the array has some extra element, do
      tempArray[k] := array[i]
      increase i and k by 1
   done
   while right part of the array has some extra element, do
      tempArray[k] := array[j]
      increase j and k by 1
   done
   return count
End

mergeSort(array, tempArray, left, right)

Input - Étant donné un tableau et un tableau temporaire, les indices gauche et droit du tableau.

Sortie - Le nombre de paires inversées après le tri.

Begin
   count := 0
   if right > left, then
      mid := (right + left)/2
      count := mergeSort(array, tempArray, left, mid)
      count := count + mergeSort(array, tempArray, mid+1, right)
      count := count + merge(array, tempArray, left, mid+1, right)
   return count
End

Exemple

Démonstration en temps réel

#include <iostream>
using namespace std;
int merge(int arr[], int temp[], int left, int mid, int right) {
   int i, j, k;
   int count = 0;
   i = left; //i to locate first array location
   j = mid; //i to locate second array location
   k = left; //i to locate merged array location
   while ((i <= mid - 1) && (j <= right)) {
      if (arr[i] <= arr[j]){ //when left item is less than right item
      temp[k++] = arr[i++];
      } else {
         temp[k++] = arr[j++];
         count += (mid - i); //find how many convertion is performed
      }
   }
   while (i <= mid - 1) //if first list has remaining item, add them in the list
      temp[k++] = arr[i++];
   while (j <= right) //if second list has remaining item, add them in the list
      temp[k++] = arr[j++];
   for (i=left; i <= right; i++)
      arr[i] = temp[i]; //store temp Array to main array
   return count;
}
int mergeSort(int arr[], int temp[], int left, int right){
   int mid, count = 0;
   if (right > left) {
      mid = (right + left)/2; //find mid index of the array
      count = mergeSort(arr, temp, left, mid); //merge sort left sub array
      count += mergeSort(arr, temp, mid+1, right); //merge sort right sub array
      count += merge(arr, temp, left, mid+1, right); //merge two sub arrays
   }
   return count;
}
int arrInversion(int arr[], int n) {
   int temp[n];
   return mergeSort(arr, temp, 0, n - 1);
}
int main() {
   int arr[] = {1, 5, 6, 4, 20};
   int n = 5;
   cout << "Number of inversions are "<< arrInversion(arr, n);
}

Sortie

Number of inversions are 2

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer