finding the minimum terms of an array

I have 64 terms and need to find the 5 smallest. At the moment, I store the 64 terms in an array, sort it, then choose the 5 smallest.

Is there a more efficient way to find the x smallest terms in an array of size n?

Comments

  • : I have 64 terms and need to find the 5 smallest. At the moment, I
    : store the 64 terms in an array, sort it, then choose the 5 smallest.
    :
    : Is there a more efficient way to find the x smallest terms in an
    : array of size n?
    :
    The fastest general way is to use the a modified bubble sort to sort the array. The modification is that you let the outer loop only run 5 times. This gives it the time of O(5*n), as opposed to quicksort O(n*ln(n)).
    If you know that the array only holds certain values, then there are sorting algorithms which are faster: O(n) time.
  • : : I have 64 terms and need to find the 5 smallest. At the moment, I
    : : store the 64 terms in an array, sort it, then choose the 5 smallest.
    : :
    : : Is there a more efficient way to find the x smallest terms in an
    : : array of size n?
    : :
    : The fastest general way is to use the a modified bubble sort to sort
    : the array. The modification is that you let the outer loop only run
    : 5 times. This gives it the time of O(5*n), as opposed to quicksort
    : O(n*ln(n)).
    : If you know that the array only holds certain values, then there are
    : sorting algorithms which are faster: O(n) time.

    Awesome! I thought of using a modified bubble sort, but I didn't know if there were better ways.

    Thank you!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories