Divide and Conquer&Count Inversions归并排序求逆序数

Divide and Conquer

The attached le Q8.txt contains 100,000 integers between 1 and 100,000 (each row has a single integer), the order of these integers is random and no integer is repeated.

刚写程序是用的visual C++,遇到了数据溢出的问题,发现long long在visual C++中不能使用,换成double之后可以,中间其实还试过了__int64,也是不可以,不过总算double可以了。Visual Studio中可以使用long long,这是我第一次使用C++,所以遇到了很多问题,仅写此篇留念。

写代码过程中,由于函数定义时忘记更改数据类型了,只修改了函数内部的,所以一开始怎么都不对,还好后面发现了,虽然用了很多的时间,但是我相信,以后一定不会再犯此类问题了,即便有也能很快发现,总之,很惭愧。

此代码中包括第一次使用的C++读取文件并放入数组。


#include
#include 
#include 
#define n 100000
int arr[100000];
using namespace std;


double MergeAndCount(int ll,int lr,int rl,int rr)
{
double count=0;
int i,j=rl,k=0;
int *tmp;
tmp = new int[rr-ll+1];
for(i=ll;i<=lr;i++)
{
while(j<=rr && arr[i]>arr[j])
tmp[k++] = arr[j++];
count += j-rl;
tmp[k++] = arr[i];
}
while(j<=rr)
tmp[k++] = arr[j++];
memcpy(arr+ll, tmp, sizeof(int)*(rr-ll+1));
delete tmp;
return count;
}


double SortAndCount(int i,int j)
{


if(i>line; data[k]=line;
}
double number = SortAndCount(0,n-1);
cout<<"The number of invertions is "<


你可能感兴趣的:(算法设计与分析)