I got a question that which type of sorting algorithm will have least time complexity when we are given an already sorted array.
3 Answers
Wikipedia has a table showing a comparison of the best, average and worst-case performance of many different sorting algorithms. Here's an excerpt:
There are plenty of algorithms that have 𝓞(𝑛) running times for best-case inputs (i.e., pre-sorted data). However, most of them (like bubble sort, for example) have 𝓞(𝑛²) running times for average and worst-case inputs. This is something you really want to avoid. Sorting a million items with one of these algorithms will take an eternity.
Fortunately, a few of these algorithms have 𝓞(𝑛 log 𝑛) average and worst-case running times. These are as follows:
I would recommend using one of these.
Comments
Sounds like a homework question, but I'd say one very simple algorithm that is time efficient on sorted or only slightly unsorted lists is bubble sort. Sorted, the time complexity is O(n). That said, 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 of O(n2).
2 Comments
Performance section of Wikipedia's bubble sort page to find out why insertion sort is probably a better choice. Insertion sort is also O(n) when the data are already sorted, and does better when the data are strongly ordered but not completely sorted.Its Insertion Sort. if you are taking a college level class they always use CLRS in the states and the answer is Insertion sort because you never enter the inner loop to do the swap if its already sorted. This results in 0 (n) because you

if(isSorted(list)) return; anySortAlgorithmYouLike(list);