7 ms·
You might consider it when you know you have small amounts of data. Say you're sorting peoples' phone numbers - not sorting all the phone numbers of all the pe
by AnimalMuppet 3y ago
You might consider it when you know you have small amounts of data.
Say you're sorting peoples' phone numbers - not sorting all the phone numbers of all the people in the system, but for each person, sorting that person's phone numbers. Well, how many phone numbers does each person have in the system? Three would be the upper limit for most people. Is bubble sort faster for n=3? Probably.
You could put a threshold - if n>20 (or some experimentally-determined number), use quicksort, else use bubble sort. You'd have to have enough instances of people with n>20 before it would even be worthwhile to make the check, though (unless it's worth it just as a sanity check/safety net).