Originally posted by: mjia
Is it? I haven't touch this "select" methord, so I won't know...perhaps it would be interesting to learn about...anyone have any links?
A selection algorithm chooses the kth smallest of a list of numbers; this is an easier problem in general than sorting. One simple but effective selection algorithm works nearly in the same manner as quicksort, except that instead of making recursive calls on both sublists, it only makes a single tail-recursive call on the sublist which contains the desired element.
Bubble sort would probably be fastest for this.
The algorithm: Compare last two elements in the array. If the first is lower than the second, leave as-is, otherwise, flip them. Do the same with second-to-last and third-to-last elements, then the third-to-last and fourth-to-last elements, etc through the entire array. In this way, the lowest value will "bubble" to the top of the array with every pass. Once you complete an entire pass through the array without switching any values, you know the whole thing is sorted.
Now, to make this work for your application, you'll want a reverse bubble sort, or a "sinking sort" if you need a cute name for it. Start at the top of the array instead and move downward for a pass, always switching values to keep the lower number on top. This will send the greatest number in the array to the bottom. After 10 passes, you know the 10 highest numbers in the array will be at the bottom of it. This is an O(n) algorithim as opposed to O(n^2) (since the time taken is only dependent on the size of the array and not the number of passes needed, which is constant).
Edit: And yes, this beats out n*log n as well. Everyone who said "insert favorite O(n log n) algorithm here" just got pwned by a sophomore electrical engineer with a CS minor. Boo-ya.
Originally posted by: sao123
Bubble sort would probably be fastest for this.
The algorithm: Compare last two elements in the array. If the first is lower than the second, leave as-is, otherwise, flip them. Do the same with second-to-last and third-to-last elements, then the third-to-last and fourth-to-last elements, etc through the entire array. In this way, the lowest value will "bubble" to the top of the array with every pass. Once you complete an entire pass through the array without switching any values, you know the whole thing is sorted.
Now, to make this work for your application, you'll want a reverse bubble sort, or a "sinking sort" if you need a cute name for it. Start at the top of the array instead and move downward for a pass, always switching values to keep the lower number on top. This will send the greatest number in the array to the bottom. After 10 passes, you know the 10 highest numbers in the array will be at the bottom of it. This is an O(n) algorithim as opposed to O(n^2) (since the time taken is only dependent on the size of the array and not the number of passes needed, which is constant).
Edit: And yes, this beats out n*log n as well. Everyone who said "insert favorite O(n log n) algorithm here" just got pwned by a sophomore electrical engineer with a CS minor. Boo-ya.
I think i said this 7 posts before you. But thanks for the confirmation... and I'm a CS major.
Originally posted by: gwlam12
Wow. Some of you aren't very modest.
Originally posted by: TheLonelyPhoenix
Originally posted by: gwlam12
Wow. Some of you aren't very modest.
Modesty is for liberal arts majors.
Originally posted by: Ameesh
just a curious side note i gave this question in an interview today and we worked on optimizing the linear solution: At this point O(n) is fine but big O represents the worst case not the avg case which is theta and as far as the number 10 goes if we generalize it to an input variable the best solution we got was n * log m which is clearly better then n * m
Originally posted by: TheLonelyPhoenix
Edit: And yes, this beats out n*log n as well. Everyone who said "insert favorite O(n log n) algorithm here" just got pwned by a sophomore electrical engineer with a CS minor. Boo-ya.
Originally posted by: chuckywang
Picking the ith element out of a set of n number is an O(n) operation. Just pick the 10 biggest elements. Your complexity is 10*O(n) which is the same as O(n).
Another easy algorithm is just to make an 10 element array that keeps track of the 10 biggest elements, and just iterate through the 100 elements, updating the 10 element array when necessary.
That give you mO(n).Originally posted by: blahblah99
Winner: a 10-iteration bubble sort. THANKS EVERYONE!
Originally posted by: blahblah99
Say I have an array of 100 elements with random numbers, and only want to keep the 10 highest numbers. What's the best way?
I don't want to sort through the whole array then pick the 10 highest, since the array can contain a million elements. Is there a variation of the sort algorithm?
TIA.
Originally posted by: chuckywang
Originally posted by: TheLonelyPhoenix
Edit: And yes, this beats out n*log n as well. Everyone who said "insert favorite O(n log n) algorithm here" just got pwned by a sophomore electrical engineer with a CS minor. Boo-ya.
I'm a senior electrical engineer, thanks.
Originally posted by: IHateMyJob2004
(1)Oh, break set of 100 elements into 10 seperate arrays(memcpy is fastest method) and sort each (bubblesort?).
(2)
Then take arrays 1,2 and find the ten lowest numbers and place them in a third array. Repeat this for 3 through 10.
(3)
now you have 5 arrays
(4)
Use simlar process as in (2) to end up with 3 arrays of size 10.
(5)
Use simlar process as in (4) to end up with 2 arrays of size 10.
(6)
Use simlar process as in (5) to end up with 1 arrays of size 10.
Step (6) will give you the lowest 10 numbers.
Originally posted by: blahblah99
Winner: a 10-iteration bubble sort. THANKS EVERYONE!
Originally posted by: IHateMyJob2004
(1)Oh, break set of 100 elements into 10 seperate arrays(memcpy is fastest method) and sort each (bubblesort?).
(2)
Then take arrays 1,2 and find the ten lowest numbers and place them in a third array. Repeat this for 3 through 10.
(3)
now you have 5 arrays
(4)
Use simlar process as in (2) to end up with 3 arrays of size 10.
(5)
Use simlar process as in (4) to end up with 2 arrays of size 10.
(6)
Use simlar process as in (5) to end up with 1 arrays of size 10.
Step (6) will give you the lowest 10 numbers.