Sorting algorithms revisited

Sorting algorithm is one of the fundamental algorithms in computer science. Despite of its simple description, the sorting problem has attracted a great deal of research since the dawn of computing (bobble sort, 1956); but and new algorithms are still being invented today (library sort, 2004).


O(n2) Sorting Algorithms:

Bubble Sort

Exchange two adjacent elements if they are out of order. Repeat until array is sorted. The bubble sort is generally considered to be the most inefficient sorting algorithm. Under the best-case, the complexity is O(n). Average-case, the complexity is O(n2).

Selection Sort

The selection sort works by selecting the smallest(biggest) unsorted element remaining in the list, and then swapping it to the proper position to be filled. Selection sort requires (n-1) + (n-2) + … + 2 + 1 = n(n-1)/2 = O(n2) comparisons and only (n-1) swaps hence O(n) memory writes. Thus it can be very attractive if writes are expensive operations.

Insertion Sort

Insertion sort accomplishes its task by considering that the first item of a list is always a sorted list of size 1. We can insert each item into its proper place by moving the item past the already sorted items and repeatedly swapping it with the preceding item until it is in place. Insertion sort is relatively efficient for small and most-sorted lists, and often us used as part of more sophisticated algorithms.

Shell Sort

Shell sort is the most efficient and most complex O(n2) sorting algorithms. It is a generalization of insertion sort. It improves insertion sort by comparing elements separated by a gap of several positions. This lets an element take “bigger steps” toward its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. The last step of Shell sort is a plain insertion sort, but by then, the data is guaranteed to be almost sorted.

O(nlogn) Sorting Algorithms:

Merge Sort

Merge sort divide the unsorted list into two equal halves; then, divide each of the two sublists recursively until the size of 1; merge the two sublists back into one sorted list.

Merge sort is often the best choice for sorting a linked list: it is relatively easy to implement a merge sort in such a way that it requires only O(1) extra space, and the slow random-access performance of a linked list makes quicksort performs poorly and heapsort completely impossible. The main disadvantage of mergesort is that, when operating on arrays, it requires O(n) auxiliary space in the best case, whereas the quicksort uses only O(logn) space.

Quick Sort

Quick sort divide the unsorted list into two sub-lists by a “pivot” from the list and reorder the list so that all elements which are less than the pivot come before it and all elements greater that the pivot come after it. After the partitioning, the pivot is in its final place. Then, recursively repeat the algorithm on both halves.

Quick sort is a space-optimized version of binary tree sort. Instead of inserting items sequentially into an explicit tree, quicksort organizes them concurrently into a tree that implied by recursive calls.

Heap Sort

Heap sort is a much more efficient version of selection sort. It also works by determining the largest (or smallest) element of the list, placing it at the end (or beginning) of the list, then continuing with the rest of the list. Heap sort performs the task efficiently by using a data structure called a heap, a special type of binary tree. The root node of the heap is guaranteed to be the largest element. After removing the largest, the heap is reconstructed so that the largest element remaining move to the root. Using the heap, finding the next largest element takes O(log n) time, instead of O(n) for a linear scan as in simple selection sort.

Heap sort has the advantage of a worst-case O(nlogn) runtime. Unlike the mergesort and quicksort, it doesn’t require massive recursion or multiple array to work. This makes it the most attractive option for very large data sets of millions of items.

O(n) Sorting Algorithms:

Radix Sort

Radix sort works by processing individual digits of integers.

A least significant digit (LSD) radix sort is a fast stable sorting algorithm which can be used to sort keys in lexicographic order. Keys may be a string of characters or numerical digits. The processing of the keys begins at the rightmost and process to the leftmost. A LSD radix sort operates in O(nk) time, where n is the number of keys, and k is the average key length.

A most significant digit (MSD) radix sort starts at the leftmost and process to the rightmost. A recursively subdividing MSD radix sort works as follows: 1) Take the most significant digit of each element, sort the list based on that digit, grouping elements with the same digit into one bucket. 2) Recursively sort each bucket, starting the next digit.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章