帮我写一份C++求逆序对的代码
时间: 2023-08-31 13:33:41 浏览: 89
用C++template实现数据列表逆序数量统计教程共23
### 回答1:
好的,这是一份求逆序对的 C 语言代码:
```
#include<stdio.h>
#include<stdlib.h>
int merge(int arr[], int temp[], int left, int mid, int right)
{
int i, j, k;
int inv_count = 0;
i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right))
{
if (arr[i] <= arr[j])
temp[k++] = arr[i++];
else
{
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i=left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
int _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left)
{
mid = (right + left)/2;
inv_count = _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid+1, right);
inv_count += merge(arr, temp, left, mid+1, right);
}
return inv_count;
}
int countInversions(int arr[], int n)
{
int temp[n];
return _mergeSort(arr, temp, 0, n - 1);
}
int main()
{
int arr[] = {1, 20, 6, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Number of inversions are %d\n", countInversions(arr, n));
return 0;
}
```
这份代码使用了归并排序的思想来求解逆序对。它会分治数组并不断地把数组合并,同时统计逆序对的个数。
### 回答2:
下面是一份求逆序对的C代码:
```c
#include<stdio.h>
int merge(int arr[], int temp[], int left, int mid, int right) {
int i = left;
int j = mid + 1;
int k = left;
int count = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
count += (mid - i + 1); // 统计逆序对数量
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left; i <= right; i++) {
arr[i] = temp[i];
}
return count;
}
int mergeSort(int arr[], int temp[], int left, int right) {
int count = 0;
if (left < right) {
int mid = (left + right) / 2;
count += mergeSort(arr, temp, left, mid);
count += mergeSort(arr, temp, mid + 1, right);
count += merge(arr, temp, left, mid, right);
}
return count;
}
int countInversions(int arr[], int n) {
int *temp = (int *)malloc(sizeof(int) * n);
return mergeSort(arr, temp, 0, n - 1);
}
int main() {
int arr[] = {7, 5, 3, 9, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int inversions = countInversions(arr, n);
printf("逆序对的数量为: %d\n", inversions);
return 0;
}
```
这个代码使用归并排序的思想,通过递归地将数组分成两部分,并计算左右两部分的逆序对数量,然后合并左右两部分并计算跨越两个子数组的逆序对数量,最后返回逆序对的总数量。代码中使用了一个辅助数组来暂时存储归并排序的结果。在主函数中,我们使用了一个简单的例子来测试代码,并打印逆序对的数量。
### 回答3:
以下是一个求逆序对数量的C语言代码示例:
```c
#include <stdio.h>
int merge(int arr[], int left, int mid, int right) {
int i = left, j = mid + 1, k = 0;
int temp[right - left + 1];
int count = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
count += mid - i + 1;
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left, k = 0; i <= right; i++, k++) {
arr[i] = temp[k];
}
return count;
}
int mergeSort(int arr[], int left, int right) {
int count = 0;
if (left < right) {
int mid = (left + right) / 2;
count += mergeSort(arr, left, mid);
count += mergeSort(arr, mid + 1, right);
count += merge(arr, left, mid, right);
}
return count;
}
int main() {
int arr[] = {2, 4, 1, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int count = mergeSort(arr, 0, n - 1);
printf("逆序对数量为:%d\n", count);
return 0;
}
```
这段代码使用了归并排序算法来求解逆序对的数量。其中,merge函数用于在归并过程中统计逆序对的数量,mergeSort函数用于进行归并排序,main函数用于测试和输出结果。运行上述代码,输出结果为"逆序对数量为:3"。
阅读全文