Should I use sorting algorithms like bubble sort, insertion sort or should I use the inbuilt sort() function in c++ to sort arrays? And if I should use sort() function, why do I need to learn other sorting algorithms? I'm not understanding what is the use of such algorithms if there is an inbuilt sort function, do they have something special?
Should I use sorting algorithms like bubble sort, insertion sort or should I use the inbuilt sort() function in c++ to sort arrays?
It is your wish. You can use anything. But you need to understand the importance of time and space complexity, your requirement before using any code.
And if I should use sort() function, why do I need to learn other sorting algorithms?
The answer to this is tricky. You need to learn stuff because each sorting algorithm is different on implementation, time and space complexities. For instance, bubble sort runs in O(n^2) time while merge sort takes O(n log n) time. while space taken by bubble sort is O(1) and merge sort takes O(n) space.In terms of code complexity, you could write a bubble sort snippet in 5 minutes while merge sort could take 30 minutes.
I'm not understanding what is the use of such algorithms if there is an inbuilt sort function, do they have something special?
Yes they do. It is to do with requirements. Today , you might not need a space effecient algorithm. But when such a case arises, you may need to prefer a particular algorithm over the other. In general, you learn these concepts just to understand how the current algorithms have been developed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With