Sorting is the operation to arrange an array and collection either in numerical order (Ascending or Descending ) or Alphabetical order. Here we will discuss Various sorting techniques and their time complexities. We will know which is the best sorting technique that you should use in competitive programming. There are various ways to sort an array here is the pseudo-code is given you can implement it in your respective language.
Best Sorting Algoritm
There are Lots of factors on which basis you can determine the best sorting algorithm.
1. Execution Time - The Algorithm should run in a definite time in its worst and average cases.
2. Space - The Algorithm should not be too bulky. It should take limited space.
3. No Repetition - The algorithm should not repeat the already covered cases, which will make the code more efficient.
Types of Sorting
The Best sorting Techniques are as follows:
1. BUBBLE SORT
This is the simplest of all sorting techniques present this is just the Brute Force Algorithm of sorting. As the name suggests, sorting happens like a bubble formation on a line. Each element is compared with its adjacent element and the swapped.
Time Complexity
Best Case - Ω(n)
Average Case - θ(n^2)
Worst Case - O(n^2)
Implementation:-
INSERTION SORT
This sorting technique is not as efficient as other techniques(heap and selection sort). In this, each element is compared with all other elements. This can be a good algorithm for a small data size.
Time Complexity
Best Case - Ω(n)
Average Case - θ(n^2)
Worst Case - O(n^2)
Implementation -:
SELECTION SORT
In this sorting process, we repeatedly find the shortest element in the array by comparing each element one by and arranging them to get the sorted collection.
Time Complexity
Best Case - Ω(n^2)
Average Case - θ(n^2)
Worst Case - O(n^2)
Implementation -:
QUICK SORT
As the name suggests, It is one of the most efficient sorting techniques. It works on the divide and conquers Algorithm. We pick two pivots and take the median of them and sort the elements by partitioning the array at the median.
Time Complexity
Best Case - Ω(nlog(n))
Average Case - θ(nlog(n))
Worst Case - O(n^2)
Implementation -:
so these are the best sorting algorithms discussed above you can optimize it more to make it more efficient and less complex.