剑指Offer面试题36(Java版):数组中的逆序对

来源:互联网 发布:网络黑分提现 编辑:程序博客网 时间:2024/05/21 23:31

题目:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数

例如在数组{7,5,6,4}中,一共存在5对逆序对,分别是{7,6},{7,5},{7,4},{6,4},{5,4}。

看到这个题目,我们的第一反应就是顺序扫描整个数组。每扫描到一个数组的时候,逐个比较该数字和它后面的数字的大小。如果后面的数字比它小,则这两个数字就组成一个逆序对。假设数组中含有n个数字。由于每个数字都要和O(n)个数字做比较,因此这个算法的时间复杂度为O(n2)。我们尝试找找更快的算法。

我们以数组{7,5,6,4}为例来分析统计逆序对的过程,每次扫描到一个数字的时候,我们不能拿它和后面的每一个数字做比较,否则时间复杂度就是O(n2)因此我们可以考虑先比较两个相邻的数字。

如下图所示,我们先把数组分解称两个长度为2的子数组,再把这两个子数组分别茶城两个长度为1的子数组。接下来一边合并相邻的子数组,一边统计逆序对的数目。在第一对长度为1的子数组{7},{5}中7大于5,因此{7,5}组成一个逆序对。同样在第二对长度为1的子数组{6},{4}中也有逆序对{6,4}。由于我们已经统计了这两队子数组内部逆序对,因此需要把这两对子数组排序,以免在以后的统计过程中再重复统计。


接下来我们统计两个长度为2的子数组之间的逆序对。

我们先用两个指针分别指向两个子数组的末尾,并每次比较两个指针指向的数字。如果第一个子数组中的数字大于第二个子数组中的数字,则构成逆序对,并且逆序对的数目等于第二个子数组中的剩余数字的个数。如果第一个数组中的数字小于或等于第二个数组中的数字,则不构成逆序对。每一次比较的时候,我们都把较大的数字从后往前复制到一个辅助数组中去,确保辅助数组中的数字是递增排序的。在把较大的数字复制到数组之后,把对应的指针向前移动一位,接着来进行下一轮的比较。

经过前面详细的讨论,我们可疑总结出统计逆序对的过程:先把数组分隔成子数组,先统计出子数组内部的逆序对的数目,然后再统计出两个相邻子数组之间的逆序对的数目。在统计逆序对的过程中,还需要对数组进行排序。如果对排序算法很熟悉,我们不难发现这个排序的过程就是归并排序。

用Java代码实现上面的过程

[java] view plain copy
  1. /** 
  2.  * 逆序对: 
  3.  * 在数组中的两个数字如果前面一个数字大于后面一个数字,则这两个数字组成一个逆序对。 
  4.  * 输入一个数组,求出这个数组中的逆序对的总数。 
  5.  */  
  6. package swordForOffer;  
  7.   
  8. import java.util.ArrayList;  
  9.   
  10. /** 
  11.  * @author JInShuangQi 
  12.  * 
  13.  * 2015年8月9日 
  14.  */  
  15. public class E36InversePairs {  
  16.       
  17.     private ArrayList<Integer> assignList(ArrayList<Integer> list ,int start,int end){  
  18.         ArrayList<Integer> des = new ArrayList<Integer>();  
  19.         for(int i = start;i<end;i++){  
  20.             des.add(list.get(i));  
  21.         }  
  22.         return des;  
  23.     }  
  24.     public long mergeTwoList(ArrayList<Integer> list,int start,int half,int end){  
  25.         long count = 0;  
  26.         ArrayList<Integer> tempLeft = assignList(list,start,half);  
  27.         ArrayList<Integer> tempRight = assignList(list,half,end);  
  28.         int leftIndex = 0;  
  29.         int rightIndex = 0;  
  30.         int index = start;  
  31.         while(leftIndex < tempLeft.size() && rightIndex <tempRight.size()){  
  32.             int temp1 = tempLeft.get(leftIndex);  
  33.             int temp2 = tempRight.get(rightIndex);  
  34.             if(temp1 > temp2){  
  35.                 count+=tempLeft.size() - leftIndex;  
  36.                 list.set(index, temp2);  
  37.                 index++;  
  38.                 rightIndex++;  
  39.             }else{  
  40.                 list.set(index, temp1);  
  41.                 index++;  
  42.                 leftIndex++;  
  43.             }  
  44.         }  
  45.         for(;leftIndex < tempLeft.size();leftIndex++){  
  46.             list.set(index, tempLeft.get(leftIndex));  
  47.             index++;  
  48.         }  
  49.         for(;rightIndex <tempRight.size();rightIndex++){  
  50.             list.set(index, tempRight.get(rightIndex));  
  51.             index++;  
  52.         }  
  53.         return count;  
  54.     }  
  55.     public long getInversions(ArrayList<Integer> list,int start,int end){  
  56.         long count = 0;  
  57.         if((end-start)<= 1)  
  58.             return 0;  
  59.         int half = start+(end-start)/2;  
  60.         count += getInversions(list,start,half);  
  61.         count += getInversions(list,half,end);  
  62.         count += mergeTwoList(list,start,half,end);  
  63.         return count;  
  64.     }  
  65.     public long getInversePairs(int[] arr){  
  66.         ArrayList<Integer> al = new ArrayList<Integer>();  
  67.         for(int i = 0;i<arr.length;i++){  
  68.             al.add(arr[i]);  
  69.         }  
  70.         int end =arr.length;  
  71.         return getInversions(al,0,end);  
  72.     }  
  73.     public static void main(String[] args){  
  74.         int[] arr={7,5,6,4};  
  75.         E36InversePairs test = new E36InversePairs();  
  76.         System.out.println(test.getInversePairs(arr));  
  77.     }  
  78. }  

0 0