In programming, sorting is a method, which takes an unsorted list, and rearranges the elements in the unsorted list to a sorted list, in which has the elements in increasing or decreasing order. Furthermore, There are various ways to sort a list, and these ways are referred to some sorting algorithms. Example of sorting algorithms are: bubble sort, selection sort, insertion sort, merge sort, and quick sort (thanks to http://art5594.blogspot.ca after reading this slog, I have a better and clear understanding about sorting algorithms). All these sorting algorithms have different runtimes, which are the length an algorithm takes to complete a sorting task. Runtimes are generally depend on the size of a list, and we call this size 'n'.
Let's look at each type of sorting algorithm and see what their runtime will be under specific circumstances.
Bubble Sort
In bubble sort, the program compares adjacent pair of numbers by taking the first and second numbers from the left. If the number on the left is greater than the number on the right, then the program will swap them. after that, the program will compare the second and third numbers with the same algorithm, and this process is stopped when the largest number is to the far right. After that, the program now has to work with list of size n-1 since the largest number is to the far right. Thus the program start this process once again until the list is sorted.
Let's look at each type of sorting algorithm and see what their runtime will be under specific circumstances.
Bubble Sort
In bubble sort, the program compares adjacent pair of numbers by taking the first and second numbers from the left. If the number on the left is greater than the number on the right, then the program will swap them. after that, the program will compare the second and third numbers with the same algorithm, and this process is stopped when the largest number is to the far right. After that, the program now has to work with list of size n-1 since the largest number is to the far right. Thus the program start this process once again until the list is sorted.
Best case
The best case for bubble sort is when input a list that is already sorted, then it will not swap any number of the list because the list is sorted. The sorting algorithm just need to compare each adjacent number, and this takes O(n). Thus, the total runtime for best case bubble sort is O(n).
Average case
The average case for bubble sort involves the operation for comparing and the operation for swapping.
So comparing each number in the list takes O(n) and swapping numbers takes O(n). Therefore the total runtime for this case is O(n^2).
Worst case
The worst case for bubble sort is much the same as the average case. So total runtime would be O(n^2)
Selection sort
Selection sort is a sorting algorithm that goes through the entire list, finds the smallest number in the list and swaps it to the front of the list.
Cases
All cases for selection sort is O(n^2) because this algorithm goes through the entire list, swaps the smallest number to the front of the list, and then repeat for n-1 list. The formula is n(n-1) which is O(n^2).
Insertion sort
The best case for bubble sort is when input a list that is already sorted, then it will not swap any number of the list because the list is sorted. The sorting algorithm just need to compare each adjacent number, and this takes O(n). Thus, the total runtime for best case bubble sort is O(n).
Average case
The average case for bubble sort involves the operation for comparing and the operation for swapping.
So comparing each number in the list takes O(n) and swapping numbers takes O(n). Therefore the total runtime for this case is O(n^2).
Worst case
The worst case for bubble sort is much the same as the average case. So total runtime would be O(n^2)
Selection sort
Selection sort is a sorting algorithm that goes through the entire list, finds the smallest number in the list and swaps it to the front of the list.
Cases
All cases for selection sort is O(n^2) because this algorithm goes through the entire list, swaps the smallest number to the front of the list, and then repeat for n-1 list. The formula is n(n-1) which is O(n^2).
Insertion sort
Insertion Sort is a sorting algorithm that first takes the first two indexes say, 0 and 1, and compares the number at index 0 and 1, and swap the number at index 0 and 1 into an increasing order. After that, index 0 and 1 is considered a sorted region. the sorting algorithm will then move forward one at a time and compares the number at that index is with the numbers in the sorted region, then the sorting algorithm will insert the number at that index in the right sorted region, and repeat this process until the number at last index is checked.
Best case
If the list is already sorted, then it will not have to swap them at all. In this case, the sorting algorithm has linear runtime of O(n).
Average case
The sorting algorithm has to compare with part of the list, and find the right spot to insert the number. In this case it has a runtime of O(n^2)
Worst case
the worst case scenario is to input a sorted reverse order list. In this situation, it will have to compare with each number and insert into the front of the list, and it will have to do it for each number. In this case it has a runtime of O(n^2)
Merge sort
Merge sort is a sorting algorithm that divide the unsorted list into n sublists, and each of the sublists contains 1 number. Furthermore, a list of 1 number is considered a sorted list. After that, the sorting algorithm will repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist left. Thus the only one sublist will be the sorted list.
Cases
The best, average, and worst case for Merge sort is O(nlog(n)) because it divides the unsorted list into n sublists which takes O(log(n)) and then merges sublists to produce new sorted sublists which takes O(n)
So together, the runtime for Merge sort is O(nlog(n)).
Quick sort
Quick sorts a sorting algorithm that first pick an number from the list, call this number a pivot. Then reorder the list so that all the numbers that are less than the pivot goes to the left of the pivot, and all the numbers that greater than the pivot goes to the right of the pivot. After that, the pivot is in its final position. Then, recursively apply the above steps to the sublist of numbers with smaller numbers and separately to the sublist of numbers with greater numbers.
Best case
The runtime of best case for quick sort is O(nlog(n)), the operation that divides list into half lists takes O(log(n)), and the operation that swap left and right numbers takesO(n). So together, the total runtime is
O(nlog(n)).
Average case
The runtime of a average case is same as a best case.
Worst case
If one chooses a very bad pivot which would be the largest number or the smallest number in the list. By doing that, the sorting algorithm will put all the numbers to one side, and this process will repeated n times. In this case, the runtime for quick sort is O(n^2).
Conclusion
To sum up, analyzing different type of sorting algorithms gives me a better understanding on their runtime and convinced me that certain sorting algorithms has certain best case and worst case performance.
Sources: http://commons.wikimedia.org/wiki/File:Quicksort-example.gif
http://en.wikipedia.org/wiki/File:Bubble-sort-example-300px.gif
http://en.wikipedia.org/wiki/File:Insertion-sort-example-300px.gif
http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif
http://en.wikipedia.org/wiki/File:Merge-sort-example-300px.gif
http://en.wikipedia.org/wiki/File:Bubble-sort-example-300px.gif
http://en.wikipedia.org/wiki/File:Insertion-sort-example-300px.gif
http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif
http://en.wikipedia.org/wiki/File:Merge-sort-example-300px.gif