ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
A bubble sort might be the thing. Overall, it's rubbish, but it gets quite fast near the end of the sort. I think it might have the edge over a quicksort.
I would use a merge sort or a quick sort personally, but performance is highly dependant on how the data happens to fall.
I've found in real world performance the bubble sort rarely, if ever, will touch the performance of merge sort or quick sort, though I'm sure with some specifically structured datasets we could show bubble sort to be quite fast.
Your best solution:
try a known C library function like qsort, first. You are less likely to have problems later on. qsort has been beat on for 15+ years. Most of the problems have long since been chased out.
Test it on real data, not programmer data. If it works okay, don't worry about squeezing an extra five seconds off run-time. It is not worth your time.
If you are in a big production environment, look for commercial libraries like syncsort. Don't try to write a linear time sort on your own.
IE. Theres 20 numbers and the first 15 are sorted but the remaining 5 are still jumbled.
In reality it might be the first 99% of them are sorted, or something like that. I'm not in a big production enviornment, so I can't afford commercial licences.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.