使代码反转计数运行得更快
make code inversion counting run faster
我使用归并排序编写反转计数代码,但由于输入是一个大const
int数组,我如何使它更快?(让0 (nlogn)的系数更小?)
有一些代码技巧吗?
谢谢。
#include<iostream>
using namespace std;
int invCount(int*, int);
int merge(int*, int*, int, int*, int);
int NumberOfInversion(const int*, int);
int main(void){
const int array[] = {0, 1, 4, 3, 2};
cout << NumberOfInversion(array, 5) << "times" << endl;
return 0;
}
int invCount(int *array, int length){
if(length <= 1)return 0;
int middle = (length + 1) / 2;
int left[middle];
int right[length - middle];
for(int i = 0; i < middle; i ++)left[i] = array[i];
for(int i = middle; i < length; i ++)right[i - middle] = array[i];
return invCount(left, middle) + invCount(right, length - middle
) + merge(array, left, middle, right, length - middle);
}
int merge(int* array, int* left, int leftLength, int* right, int rightLength){
int i = 0, j = 0, count = 0;
while(i < leftLength && j < rightLength){
if (left[i] <= right[j]){
array[i + j] = left[i];
i ++;
}
else {
array[i + j] = right[j];
j ++;
count += leftLength - i;
}
}
if(i == leftLength){
while(j < rightLength){
array[i + j] = right[j];
j ++;
}
}
else{
while(i < leftLength){
array[i + j] = left[i];
i ++;
}
}
return count;
}
int NumberOfInversion(const int *A, int N)
{
int input[N];
for(int i = 0; i < N; i ++)input[i] = A[i];
int result = invCount(input, N);
return result;
}
ps:大约20%的可能对是倒转的
这是一个计数反转的C代码
#include <stdio.h>
#include <stdlib.h>
int _mergeSort(int arr[], int temp[], int left, int right);
int merge(int arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
int mergeSort(int arr[], int array_size)
{
int *temp = (int *)malloc(sizeof(int)*array_size);
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
int _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left)
{
/* Divide the array into two parts and call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left)/2;
/* Inversion count will be sum of inversions in left-part, right-part
and number of inversions in merging */
inv_count = _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid+1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid+1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays and returns inversion count in
the arrays.*/
int merge(int arr[], int temp[], int left, int mid, int right)
{
int i, j, k;
int inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* i is index for right subarray*/
k = left; /* i is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right))
{
if (arr[i] <= arr[j])
{
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];
/*this is tricky -- see above explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i=left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
/* Driver progra to test above functions */
int main(int argv, char** args)
{
int arr[] = {1, 20, 6, 4, 5};
printf(" Number of inversions are %d n", mergeSort(arr, 5));
getchar();
return 0;
}
这里给出了详细的解释:http://www.geeksforgeeks.org/counting-inversions/
相关文章:
- 如何在linux终端中同时编译和运行c++代码
- 尝试用java代码编译和运行c++代码
- 不同/较旧的处理器运行c++代码的方式是否不同
- 无法在 geany 上运行代码。无法执行构建命令
- 运行代码时,c++ 会终止进程
- 编译多个C++文件.调用二进制文件以运行代码
- 分段故障 运行C++代码时出现 SIGSEGV
- 如何分析在 Windows 上运行C++代码?
- 编译器资源管理器(godbolt)如何安全地运行代码?
- 如何在 vim (windows) 中编译和运行 c++ 代码
- 无法在 Visual Studio 代码中生成和运行 C++ 代码
- 连续运行C++代码和 Python 代码
- 在静态初始化期间运行代码
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。我该如何解决这个问题?
- 如何在Visual Studio Code的终端内运行C++代码?
- 在注入的 Dll 的 DllMain 中运行代码导致注入超时
- 并行和顺序运行代码时会产生不同的结果
- 运行代码时C++错误
- 在 CortexM0 中从 RAM 运行代码时出现硬故障