: 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.
Comments
: 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.
: : 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!