分治法(divide_and_conquer),通俗的来说,要想统治一片领土,可以把这片领土进行分解成若干块小部分,然后一块块地征服,直到所有的小土地都被征服了之后,意味着征服了这一片领土。
分治法的思想分为三部分:
二:分治法的应用
(1)归并排序:归并排序是采用分治法的一个非常典型的例子
分(divide):将序列一中点为中心一分为二,分为左区间和右
治(conquer):把左右两个子区间分别进行排序
合(combine):最后把左区间和右区间合并成有序序列
T(n)=2T(n/2)+ Θ(n)= Θ(nlgn)
其中,T(n/2)表示递归的复杂度,2表示分为了左右两个区间,Θ(n)表示合并n个元素的复杂度
import java.util.Arrays;
/*
* 归并排序
*/
public class MergeSort {
public static void merge(int []a ,int low,int mid,int high)
{
int[] test = Arrays.copyOf(a, high-low+1);
int i = low;
int j = mid + 1;
int k = 0;
// 把较小的数先移到新数组中
while (i <= mid && j <= high) {
if (a[i] < a[j]) {
test[k++] = a[i++];
} else {
test[k++] = a[j++];
}
}
// 把左边剩余的数移入数组
while (i <= mid) {
test[k++] = a[i++];
}
// 把右边边剩余的数移入数组
while (j <= high) {
test[k++] = a[j++];
}
// 把新数组中的数覆盖nums数组
for (int k2 = 0; k2 < test.length; k2++) {
a[k2 + low] = test[k2];
}
}
public static void mergeSort(int[] a,int low,int high)
{
int mid=low+(high-low)/2;
if(low>=high) return;
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
//归并
merge(a,low,mid,high);
System.out.println(Arrays.toString(a));
}
public static void main(String[] args)
{
int a[] = {48,51,69,81,23,26,54,85,64,91};
mergeSort(a,0,a.length-1);
System.out.println("排序之后" + Arrays.toString(a));
}
/*
* 二分查找
*/
public class BinarySearch {
private BinarySearch(){}
public static int binarySearch(Comparable[] arr,int low,int high,Comparable target)
{
int mid=(high-low)/2+low;
while(low<=high)
{
if(arr[mid].compareTo(target)==0)
{
return mid;
}
if(target.compareTo(arr[mid])>0)
{
return binarySearch(arr,mid+1,high,target);
}
else
{
return binarySearch(arr,low,mid-1,target);
}
}
return -1;
}
public static void main(String[] args) {
Integer srcArray[] = {23, 26, 48, 51, 54, 64, 69, 81, 85, 91};
System.out.println(binarySearch(srcArray,0,srcArray.length,81));
}
}
(3)
乘方问题:求x的n次方
public class ChengFangWenTi {
public static long power1(long x,long n)
{
if(n==1)
{
return x;
}
return x*power(x,n-1);
}
public static long power(long x,long n)
{
if(n==1)
{
return x;
}
if(n%2==1) //当n为奇数的时候
{
return x*power(x*x,(n-1)/2);
}
return power(x*x,n/2); //当n为偶数的时候
}
public static void main(String [] args)
{
long x=2,n=10;
long a=power(x, n);
System.out.println(a);
}
}
(4)斐波那契数列/*
* 斐波那契数列:又叫兔子数列
* F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)
*/
public class Fibonacci {
public static int show(int n){
if(n==0)
{
return 0;
}
if(n==1){
return 1;
}
if(n==2){
return 1;
}
return show(n-1)+show(n-2);
}
public static void main(String[] args)
{
int n=10;
System.out.println(show(n));
}
}