Sorting algorithm is computer program or algorithm that takes a list of items and puts them in a specific order. Python includes a number of sorting algorithms, which can be accessed using the bisect module. The most commonly used sorting algorithms are insertion sort, merge sort, and heap sort. The sorting algorithm can be selected using the algorithm keyword argument.
Complexity of Sorting Algorithm
I would like to cover three cases regarding sorting algorithm i.e. The best, the worst and the average case in terms of complexity of code.
The unrealistically assuming that the running time of an algorithm is dependent only on input size and not the actual contents of the input. The example of a sorting algorithm is given to illustrate this point.
def sorting_algorithm(seq):
n = len(seq)
for i in range(n-1):
if seq[i] > seq[i+1]:
break
else:
return ...
The sequence is checked to see if it is already sorted. If it is, the function simply returns.
Hence, the running time of a sorting algorithm, noting that it is linear when the sequence is already sorted. However, this is an anomaly and that in general, no sorting algorithm can achieve linear running time.
I would like to suggest that the solution to a quandary is to be more specific when discussing a problem. This can be done by narrowing down the input to a unique problem, a problem in a well-defined class, or a problem in a computationally tractable class.
The best case in sorting algorithm
Think a little here when no work needs to be done to find the correct position of each element.
The best-case running time is the running time you get when the input is optimally suited to your algorithm. For example, if the input sequence to sorting_algorithm() were sorted, we would get the best-case running time (which would be linear) because no work needs to be done to find the correct position of each element.
The worst case in sorting algorithm
The worst case of an algorithm is the longest possible running time it could theoretically take to complete. This is usually the most useful case to consider when trying to understand the efficiency of an algorithm, as it gives the best possible estimate for how long the algorithm might take to run in practice.
The average case in sorting algorithm
The average case is the expected value of the running time for random input. This should be avoided.
The best-case running time of our algorithm is Θ(n) (when the check uncovers a sorted sequence),and the worst-case running time is Θ(n lg n). Many of the algorithms we’ll be working with have the same complexity in the three cases. When they don’t, we’ll often be working with the worst case.
Unless this is stated explicitly, however, no assumptions can be made about which case is being studied. In fact, we may not be restricting ourselves to a single kind of input at all. As we are learning the coding and understanding the best to be the best developers in the world.
The running time of a program can vary greatly depending on the type of input. Different types of inputs have different running time functions, which cannot be summed up in a single expression. The solution is to use O or Ω to denote the tightest possible bound on the running time instead of Θ.
You might be interested to read about Python coding best practices: complexity of code: Algorithm analysis and design
Types of sorting algorithms
Let us break down sorting algorithm in different types of sorting. The following code is just a baby coding to show the sorting types:
def sorting_algorithm():
print("Sorting Algorithm")
print("1. Bubble Sort")
print("2. Insertion Sort")
print("3. Selection Sort")
print("4. Merge Sort")
print("5. Quick Sort")
print("6. Heap Sort")
print("7. Radix Sort")
print("8. Exit")
print("Enter your choice: ", end="")
choice = int(input())
if choice == 1:
print("Bubble Sort")
elif choice == 2:
print("Insertion Sort")
elif choice == 3:
print("Selection Sort")
elif choice == 4:
print("Merge Sort")
elif choice == 5:
print("Quick Sort")
elif choice == 6:
print("Heap Sort")
elif choice == 7:
print("Radix Sort")
elif choice == 8:
print("Exit")
else:
print("Invalid choice")
# lets execute it to find out different types of sorting
sorting_algorithm()
The above simple baby program shows us the different types of sorting i.e. Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, Heap Sort, Radix Sort. These all are explained very well in other post. In fact I am avoiding this post to be lengthy.
Conclusion
I did my best to focus on the best, worst and an average cases regarding complexity of code in terms of best coding practices. All sorting types have been discussed thoroughly in the other post as you could click the link in the last paragraph just above conclusion. Thanks for reading.