Find the x smallest integers in a list of length n

前端 未结 12 1750
时光取名叫无心
时光取名叫无心 2021-02-02 01:00

You have a list of n integers and you want the x smallest. For example,

x_smallest([1, 2, 5, 4, 3], 3) should return [1, 2, 3].

I\'ll v

相关标签:
12条回答
  • 2021-02-02 01:09
        private static int[] x_smallest(int[] input, int x)
        {
            int[] output = new int[x];
            for (int i = 0; i < x; i++) { // O(x)
                output[i] = input[i];
            }
    
            for (int i = x; i < input.Length; i++) { // + O(n-x)
                int current = input[i];
                int temp;
    
                for (int j = 0; j < output.Length; j++) { // * O(x)
                    if (current < output[j]) {
                        temp = output[j];
                        output[j] = current;
                        current = temp;
                    } 
                }
            }
    
            return output;
        }
    

    Looking at the complexity: O(x + (n-x) * x) -- assuming x is some constant, O(n)

    0 讨论(0)
  • 2021-02-02 01:16

    Psudocode:

    def x_smallest(array<int> arr, int limit)
        array<int> ret = new array[limit]
    
        ret = {INT_MAX}
    
        for i in arr
            for j in range(0..limit)
                if (i < ret[j])
                    ret[j] = i
                endif
            endfor
        endfor
    
        return ret
    enddef
    
    0 讨论(0)
  • 2021-02-02 01:19

    If the range of numbers (L) is known, you can do a modified counting sort.

    given L, x, input[]
    counts <- array[0..L]
    for each number in input
        increment counts[number]
    next
    
    #populate the output
    index <- 0
    xIndex <- 0
    while xIndex < x and index <= L
       if counts[index] > 0 then
           decrement counts[index]
           output[xIndex] = index
           increment xIndex
       else
           increment index
       end if
    loop
    

    This has a runtime of O(n + L) (with memory overhead of O(L)) which makes it pretty attractive if the range is small (L < n log n).

    0 讨论(0)
  • 2021-02-02 01:21

    You can find the k-th smallest element in O(n) time. This has been discussed on StackOverflow before. There are relatively simple randomized algorithms, such as QuickSelect, that run in O(n) expected time and more complicated algorithms that run in O(n) worst-case time.

    Given the k-th smallest element you can make one pass over the list to find all elements less than the k-th smallest and you are done. (I assume that the result array does not need to be sorted.)

    Overall run-time is O(n).

    0 讨论(0)
  • 2021-02-02 01:23
    def x_smallest(items, x):
        result = sorted(items[:x])
        for i in items[x:]:
            if i < result[-1]:
                result[-1] = i
                j = x - 1
                while j > 0 and result[j] < result[j-1]:
                    result[j-1], result[j] = result[j], result[j-1]
                    j -= 1
        return result
    

    Worst case is O(x*n), but will typically be closer to O(n).

    0 讨论(0)
  • 2021-02-02 01:24

    What about using a splay tree? Because of the splay tree's unique approach to adaptive balancing it makes for a slick implementation of the algorithm with the added benefit of being able to enumerate the x items in order afterwards. Here is some psuedocode.

    public SplayTree GetSmallest(int[] array, int x)
    {
      var tree = new SplayTree();
      for (int i = 0; i < array.Length; i++)
      {
        int max = tree.GetLargest();
        if (array[i] < max || tree.Count < x)
        {
          if (tree.Count >= x)
          {
            tree.Remove(max);
          }
          tree.Add(array[i]);
        }
      }
      return tree;
    }
    

    The GetLargest and Remove operations have an amortized complexity of O(log(n)), but because the last accessed item bubbles to the top it would normally be O(1). So the space complexity is O(x) and the runtime complexity is O(n*log(x)). If the array happens to already be ordered then this algorithm would acheive its best case complexity of O(n) with either an ascending or descending ordered array. However, a very odd or peculiar ordering could result in a O(n^2) complexity. Can you guess how the array would have to be ordered for that to happen?

    0 讨论(0)
提交回复
热议问题