Open In App

Selection Algorithms

Last Updated : 18 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Selection Algorithm is an algorithm for finding the kth smallest (or largest) number in a list or an array. That number is called the kth order statistic. It includes the various cases for finding the minimum, maximum and median elements in a list or an array. For finding the minimum (or maximum) element by iterating through the list, we keep the track of current minimum (or maximum) elements that occur so far and it is related to the selection sort.
Below are the different ways of selecting the kth smallest(or largest) element in an unordered list:

  1. Selection by Sorting
    Sorting the list or an array then selecting the required element can make selection easy. This method is inefficient for selecting a single element but is efficient when many selections need to be made from an array for which it only needs sorting of an array. For selection in a linked list is O(n) even if the linked list is sorted due to lack of random access.
    Instead of sorting the entire list or an array, we can use partial sorting to select the kth smallest(or largest) element in a list or an array. Then the kth smallest(or largest) is the largest(or smallest) element of the partially sorted list. This takes O(1) to access in an array and O(k) to access in a list.

    • Unordered Partial Sorting
      Unordered partial sorting is a sorting algorithm in such a way that first k element are in sorted order and rest element are in random order. For finding the kth smallest(or largest) element. The time complexity reduces to O(k log k). But since K ≤ n, The asymptotic Time Complexity converges to O(n).
      Note: Due to the possibility of equality of elements, one must not include the element less than or equals to the kth element while sorting a list or an array, as element greater than the kth element may also be equal to the kth smallest element.
    • Partial selection sort
      The concept used in Selection Sort helps us to partially sort the array up to kth smallest(or largest) element for finding the kth smallest(or largest) element in an array. Thus a partial selection sort yields a simple selection algorithm that takes O(k*n) time to sort the array. This is asymptotically inefficient but can be sufficiently efficient if k is small, and is easy to implement.
      Below is the algorithm for partial selection sort:

      function partialSelectionSort(arr[0..n], k) {
          for i in [0, k) {
              minIndex = i
              minValue = arr[i]
              for j in [i+1, n) {
                  if (arr[j] < minValue)  then
                      minIndex = j
                      minValue = arr[j]
                      swap(arr[i], arr[minIndex])
              }  
          }
          return arr[k]
      }
      
  2. Partition Based Selection
    For partition-based selection, the Quick select Algorithm is used. It is a variant of quicksort algorithm. In both, we choose a pivot element and using the partition step from the quicksort algorithm arranges all the elements smaller than the pivot on its left and the elements greater than it on its right.
    But while Quicksort recurses on both sides of the partition, Quickselect only recurses on one side, the side on which the desired kth element is present.
    The partition-based algorithms are done in place, which results in partially sorting the data. They can be done out of place by not changing the original data at the cost of O(n) auxiliary space.
  3. Median selected as pivot
    A median-selection algorithm can be used to perform a selection algorithm or sorting algorithm, by selecting the median of the array as the pivot element in Quickselect or Quicksort algorithm.
    In practice the overhead of pivot computation is significant, so these algorithms are generally not used, but this technique is of theoretical interest in relating selection and sorting algorithms.
    The median of an array is the best pivot for sorting of an array because it evenly divides the data into two parts., and thus guarantees optimal sorting, assuming the selection algorithm is optimal.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads