程式扎記: [ Data Structures with Java ] Section 7.4 : Finding kth-Largest Element

標籤

2011年1月24日 星期一

[ Data Structures with Java ] Section 7.4 : Finding kth-Largest Element

Preface : 
A application might need to find the median value in an array. The median is a value M such that half the array elements are <= M and remaining values are >= M. For instance, the Bureau of Labor Statistics reports the median family income as the middle value in a random sample of family incomes. Finding a median is a special case of a more general problem that locates the kth-largest element of an array. Out destination "kth largest" implies that value at index k. The 0th largest element is thus the smallest element. We can solve the problem of finding the kth-largest element by first sorting the array and then simply accessing the element at position k. This is an inefficient algorithm, however, because the sort requires additional work to order all of the elements. We simply need to locate the position of the kth-largest value in the list by partitioning the elements into two disjoint sublists. The lower sublist must contain k elements that are less than or equal to kth Largest, and the upper sublist must contains elements that are greater than or equal to kth Largest. Below figure illustrates the partition. The elements in the lower sublist do not need to be ordered only have values that are less than or equal to kth Largest. The opposite condition applies to the upper sublist : 
 

Method findKth() : 
We will apply the "pivoting" technique from the quicksort algorithm to create the partition. The method findKth() takes an array arr, the indices for the index range [first, last), and the position k as arguments. It modifies the array so that the kth largest element is at index k. The work is done by calling pivotIndex() from within the recursive method findKth(). Each call to findKth() takes the array and an index range and has pivotIndex() rearrange elements greater than or equal to the pivot value. If the index is k, then the pivot is the kth-largest element, and the method findKth() return because it has reached the stopping condition. If the index is greater than k, then a recursive call to findKth() with index range [first, index) continues the search for kth largest element in the lower sublist. Otherwise, a recursive call to findKth() with index range [index+1, last) continues the search in the upper sublist : 

- 函式 findKth() 範例代碼 :
  1. public static extends Comparablesuper T>> void findKth(T[] arr, int first, int last, int k) {  
  2.     if(k>=first && k
  3.         int index = pivotIndex(arr, first, last);  
  4.         if(index==k) return;  
  5.         else if(index < k)findKth(arr, index+1, last, k);  
  6.         else findKth(arr, first, index, k);  
  7.     }  
  8. }  

Running Time of findKth() : 
This algorithm should have a faster running time than quicksort, because it rejects either the lower or upper sublist at each stage. Let us intuitively develop its running time for an n-element array. Assume, as we did for quicksort, that the pivot always lies at the midpoint of the sublist. Under this assumption, locating the first pivot requires approximately n comparisons, finding the second pivot requires approximately n/2 comparisons and so on. The whole process requires no more than : 
n + n/2 + n/4 + n/8 + ... = n (1 + 1/2 + 1/4 + 1/8 + ...) = 2n 

This intuitive argument indicates that the average running time for findKth() is O(n), which is linear. If the pivot is always the largest of smallest element int its sublist, findKth() has the same worst-case running time as quicksort, which is O(n^2).

沒有留言:

張貼留言

網誌存檔