Quick select

Quick select 


https://www.youtube.com/watch?v=1vrdZiVmqno



https://www.geeksforgeeks.org/quickselect-algorithm/


http://www.geekviewpoint.com/java/search/quickselect
With unit test 

But this code is kinda weird, in the partition steps, it’s only I++, not j - - 


Quickselect is a selection algorithm to find the k-th smallest element in an unordered list. It is related to the quick sort sorting algorithm.



The algorithm is similar to QuickSort. The difference is, instead of recurring for both sides (after finding pivot), it recurs only for the part that contains the k-th smallest element. The logic is simple, if index of partitioned element is more than k, then we recur for left part. If index is same as k, we have found the k-th smallest element and we return. If index is less than k, then we recur for right part. This reduces the expected complexity from O(n log n) to O(n), with a worst case of O(n^2).



Generally, selection algorithms are modified sort algorithms; where
 *   instead of sorting the whole list, we sort up to the kth value.
 *   Hence, a selection algorithm is bounded by whatever sort algorithm
 *   is used to implement it.
 * 
 *   Here for example we are using quickselect to find the kth largest
 *   value. Consequently, this algorithm is bounded by quicksort; leading
 *   to a worse case time complexity of O(n^2) and an average case
 *   time complexity of O( n log n).




public int quickSelect(int[] array, int k){
  return helper(array, 0, array.length - 1, k - 1);
}

private int quickSelect(int[] array, int first, int last, int k){
  if(first > last){
    return Integer.MIN_VALUE;
  }else{
    //first <= last
    int pivot = partition(array, first, last);
    if(pivot == k){
      return array[k];
    }
    if(pivot > k){
      return quickSelect(array, first, pivot - 1, k);
    }
    // else , pivot < k 
    return quickSelect(array, pivot + 1, last, k);
  }
}

private int partition(int[] array, int first, int last){
  int pivot = first + new Random().nextInt(last - first + 1);
  // move the pivot to the end of the array
  swap(array, last, pivot);
  // while ( first < last)
  //    if(first < array[pivot], first++
  //    if(last > array[pivot], last--
  //    else swap(last, first, array)
  
  for(int i = first; i < last; i++){
    if(array[i] > array[last]){
      swap(array, i, first);
      first++;
    }
  }
  // move the pivot to a approriate pos
  swap(array, first, last);
  return first;
}

private void swap(int[] array. int x, int y){
  int tmp = array[x];
  array[x] = array[y];
  array[y] = tmp;
}




// unit test 

 
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
 
public class SearchAlgorithmsTest {
   
  /**
   * Test of Quickselect method, of class SearchAlgorithms.
   */
  @Test
  public void testQuickselect() {
    System.out.println("quickselect");
    int[] A = {21, 3, 34, 5, 13, 8, 2, 55, 1, 19};
    SearchAlgorithms search = new SearchAlgorithms();
    int expResult[] = {1, 2, 3, 5, 8, 13, 19, 21, 34, 55};
    int k = expResult.length;
    for (int exp:expResult) {
      assertEquals(exp, search.quickselect(A, k--));
    }
  }
}

 

posted on 2018-09-20 18:00  猪猪&#128055;  阅读(317)  评论(0)    收藏  举报

导航