Least time complexity sorting algorithm for an already sorted array?
Sounds like a homework question, but I'd say one very simple algorithm, time efficient for sorted or only slightly unsorted lists, is a bubble type . Sorting, time complexity O (n). However, there are many sorting algorithms that have similar time complexity for the best-case scenario (i.e. already sorted), and bubble sort has a worst-case O (n 2 ).
source to share
Wikipedia has a table showing a comparison of the best, average and worst performance of many different sorting algorithms . Here's an excerpt:
There are many algorithms that have (𝑛) running times for the best inputs (i.e. pre-sorted data). However, most of them (eg bubble sort) have 𝓞 (²²) run times for medium and worst-case inputs. This is what you really want to avoid. Sorting a million items with one of these algorithms will take forever.
Fortunately, some of these algorithms have average and worst-case running times of 𝓞 (𝑛 log 𝑛). These are the following:
I would recommend using one of them.
source to share