1) Bubble Sort:
This algorithm starts by looking at the first two elements in a list and checks to see if they are out of order relative to each other. If yes, it swaps them, otherwise it leaves them where they are. Next, the algorithm looks at the second and third element of the list and repeats the same procedure on them. This continues until the the algorithm goes through the full list. After a complete pass through the list, this entire procedure happen again and again on the list until the list is completely sorted.
Complexity: O(n2)
2) Insertion Sort:
This algorithm divides the list into 2 parts, an unsorted part and a sorted part. At each step of the algorithm an element moves from the unsorted part to the sorted part until the entire list is sorted. The algorithm starts by considering the entire list to be unsorted and moving the first element of the unsorted part to the sorted part. After the placement of the first element in the sorted part, it compares the first element of the unsorted part to the entire sorted part and places it in the right position. As the unsorted element goes through the sorted part of the list until it finds the its right place, the elements in the sorted part shift up a place in order to make room for the new number. This procedure continues until no unsorted element is left and the entire list is sorted.
Complexity: O(n2)
3) Selection Sort:
Selection Sort also divides the list into 2 sorted and unsorted parts. The difference from Insertion Sort is that instead of comparing the first element of the unsorted part to the sorted part, it takes the smallest element of the unsorted part, and just simply appends it to the end of the sorted part each iteration through the list. This procedure continues until there is only 1 element left in the unsorted part which means all of the other elements are sorted and since the unsorted element is greater than all of them, the entire list is sorted.
Complexity: O(n2)
4) Quick Sort:
This algorithm starts by choosing the last element of a list as the pivot and puts an imaginary wall before the first element. The goal is to divide the list into 2 part of smaller, and greater than pivot. After the pivot is chosen, the algorithm looks at every element in the right of the wall and compares it with pivot. If the element is smaller than the pivot, it swaps that element with the first element on the right of the wall and then shifts the wall up by one index, otherwise, it leaves the element there and goes to the next one. This is repeated until the algorithm reaches the pivot, then it swaps the pivot with the first element on the the wall. Now, the pivot is in its final position and this algorithm is recursively called on both the smaller and the part of the pivot until the full list is covered and is in the sorted order.
Complexity: O(n2)
5) Merge Sort:
Merging is the process of creating a single sorted list of two other sorted lists. The algorithm for Merge compares the first element of the two lists and moves the smallest one to the merged list. This process continues until all the elements are in the merged list. The Merge Sort algorithm just recursively divides the list into roughly 2 equal parts until there is only element in each list and then merges them together which results in a sorted list.
Complexity: O(nlog(n))
By comparing the complexities of these algorithms, we can see the difference between the worst case run-time that it take for them to sort a really large list. From the lab exercises and comparison that we did in the course, we saw that bubble sort has the worst performance and it is followed by insertion and selection sort. Also, even though the worst case run-time of Quick Sort is O(n2), that only happens in 1 case when choose the worst pivot possible each iteration through the list. This explains the reason that average case of Quick sort is only O(nlog(n)). We also cannot conclude that Merge Sort will perform faster for sorting every list. As it was said before, these complexities are for really large list and in fact, that is what is important to us programmers since the difference in run-time of any of these algorithm on a short list would not be really noticeable. It should be also remembered that faster algorithms like Merge Sort do not come without the price. Unlike Bubble, Insertion or Selection Sort, Merge sort produces a new list instead of modifying the original one and it takes some additional space in the memory for that. This space is a bit of a price, but it usually is reasonable.