这一节允接上一节的代码
分治策略与递归: link.
寻找最小差值
通过划分去寻找最小差值,首先将数组划分,将其划分至最小的时候进行差值比较,接着随着递归并不断进行差值比较,从而得到最小差值
int Partition(int* br, int left, int right) //划分函数
{
int tmp = br[left];
while (left < right)
{
while (left < right && br[right] > tmp)
{
--right;
}
if (left < right)
{
br[left] = br[right];
}
while (left < right && br[left] <= tmp)
{
++left;
}
if (left < right)
{
br[right] = br[left];
}
}
br[left] = tmp;
return left;
}
int FindK(int* br, int left, int right, int k)
{
if (left == right && k == 1)
{
return br[left];
}
int pos = Partition(br, left, right);//划分函数
//left right是物理下标 k是逻辑下标
int j = pos - left + 1;//该物理位置属于第几小
if (k <= j)
{
return FindK(br, left, pos, k);
}
else
{
return FindK(br, pos + 1, right, k - j);
}
}
int MaxS1(const int* br, int left, int right)
{
return br[right];
}
int MinS2(const int* br, int left, int right)
{
int mins = br[left];
for (int i = left + 1; i <= right;i++)
{
if (mins > br[i])
{
mins = br[i];
}
}
return mins;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
int Min(int a, int b, int c)
{
return Min(a, Min(b, c));
}
int Cpair(int* br, int left, int right)
{
if (right - left <= 0) return INT_MAX;
int mid = (right - left + 1) / 2;
FindK(br, left, right, mid); //找到正确中间值位置
int pos = left + mid - 1;
int d1 = Cpair(br, left, pos); //左划分
int d2 = Cpair(br, pos + 1, right); //又划分
int maxs = MaxS1(br, left, pos);
int mins = MinS2(br, pos + 1, right);
return Min(d1, d2, mins - maxs);//左集合差值,右集合差值,两集合差值
}
int Cpair_Ar(int* br, int n)
{
if (br == NULL || n < 2) return INT_MAX;//无穷大
else return Cpair(br, 0, n - 1);
}
int main()
{
int ar[] = { 12,34,78,90,23,45,56,89,100,67 };
int n = sizeof(ar) / sizeof(ar[0]);
int dist = Cpair_Ar(ar, n);
cout << dist << endl;
return 0;
}
归并排序的非递归实现
void Print_Ar(int* br, int n)
{
for (int i = 0; i < n; i++)
{
cout << br[i] << " ";
}
cout << endl;
}
void Merge(int* src, int* dest, int left, int m, int right)
{
int i = left, j = m + 1;
int k = left;
while (i <= m && j <= right)
{
dest[k++] = src[i] <= src[j] ? src[i++] : src[j++];
}
while (i <= m)//j->right 结束
{
dest[k++] = src[i++];
}
while (j <= right)//或者 i->m 结束
{
dest[k++] = src[j++];
}
}
void MergePass(int* src, int* dest, int n, int s)
{
int i = 0;
for (i = 0; i + 2 * s - 1<= n - 1; i += s * 2) //保证可以做归并 i+2*s<=n 可以再次进行一次归并
{ //初始 左块的末尾 右块末尾
Merge(src, dest, i, i + s - 1, i + 2 * s - 1);
}
if (n - 1 > i + s - 1) //加一次s不超过总长
{
Merge(src, dest, i, i + s - 1, n - 1);//够两个块归并
}
else
{
for (int j = i; j < n; j++)
{
dest[j] = src[j]; //n<i+s 不足归并一个块大小,直接进行拷贝
}
}
}
void MergeSort(int* br, int n)
{
if (br == NULL || n < 2) return;
int* tmp = new int[n];
int s = 1; //块的大小
while (s < n)
{
MergePass(br, tmp, n, s);
s += s;
MergePass(tmp, br, n, s);
s += s;
}
delete[]tmp;
}
int main()
{
int ar[] = { 12,34,78,90,23,45,56,89,100,67 };
int n = sizeof(ar) / sizeof(ar[0]);
Print_Ar(ar, n);
MergeSort(ar, n);
Print_Ar(ar, n);
return 0;
}
全排列问题
void Perm(int* br, int i, int m)
{
if (i == m)
{
for (int j = 0; j <= m; j++)
{
cout << br[j] << " ";
}
cout << endl;
}
else
{
for (int j = i; j <= m; j++)
{
std::swap(br[i], br[j]);//第一次交换固定
Perm(br, i + 1, m);
std::swap(br[i], br[j]);//第二次交换归位
}
}
}
int main()
{
int ar[] = { 1,2,3 };
int n = sizeof(ar) / sizeof(ar[0]);
Perm(ar, 0, n - 1);
}
对于分治策略问题,仅仅需要考虑一层的策略,并将其进行分治划分,将大量问题划分为小的问题,其主要关注的就是如何划分以及如何截至
求集合子集
void Print(int *ar,int* br, int i, int n)
{
if (i > n)
{
for (int j = 0; j <= n; j++)
{
if (br[j] == 1)
{
cout << ar[j] << " ";
}
}
cout << endl;
}
else
{
br[i] = 1;
Print(ar, br, i + 1, n);
br[i] = 0;
Print(ar, br, i + 1, n);
}
}
int main()
{
int ar[] = { 1,2,3 };
int br[] = { 0,0,0 };
int n = sizeof(ar) / sizeof(ar[0]);
Print(ar, br, 0, n - 1);
}
根据深度遍历算法,通过递归来求集合子集,以层次观点去看一层的关系