# Introduction

Sorting is algorithms that put elements of an array in a certain order. Efficient sorting is important for what require sorted data.

In this blog, the assumption for these algorithms is that the entire sort can be done in main memory.

# Comparison

A basic comparison of some algorithms:

Name | Best | Average | Worst | Memory | Stable | Comment |
---|---|---|---|---|---|---|

Insertion sort | Ω(N) | Θ(N^{2}) |
O(N^{2}) |
1 | Yes | |

Shell sort | Ω(N*log(N)) | Depends on gap sequence | Depends on gap sequence; best known is O(N^{4/3}) |
1 | No | |

Heapsort | Ω(N*log(N)) (N if all keys are not distinct) | Θ(N*log(N)) | O(N*log(N)) | 1 | No | |

Mergesort | Ω(N*log(N)) | Θ(N*log(N)) | O(N*log(N)) | n | Yes | |

Quicksort | Ω(N*log(N)) | Θ(N*log(N)) | O(N^{2}) |
log(N) | Both | |

Bucket Sort | Ω(N+M) | Θ(N+M) | O(N^{2}) |
N+M | Yes | Use other sorting algorithms to sort buckets |

Bubble Sort | Ω(N) | Θ(N^{2}) |
O(N^{2}) |
1 | Yes | |

Selection Sort | Ω(N^{2}) |
Θ(N^{2}) |
O(N^{2}) |
1 | No |

## Insertion Sort

Insertion sort is one of the simplest sorting algorithm that is quite suitable for small data but is less efficient on large data.

The steps of insertion sort are simple:

- Loop the element from the second to the last.
- Compare the element to the elements before it, and switch the places until it is larger.

(Source: Wikipedia and Wikimedia)

## Shell Sort

Shell sort is a generalization of insertion sort that allows the exchange of elements that are far apart.

The steps of shell sort are:

- Sort pairs of elements far apart from each other by gap.
- Reduce the gap and repeat until gap is 1.

(Source: Wikipedia and Wikimedia)

The worst case of shell sort is complicated and depends on the gap sequence.

## Heapsort

Heap sort is a comparison-based sorting algorithm using binary heap data structure.

The steps of heap sort are:

- Build a max heap from the array. Then the largest element is at the root of the heap.
- Swap the first element with the last element.
- Build a max heap without considering the last element.
- Then repeat the step 2 and step 3 until the considered range of elements is only one.

(Source: Wikipedia and Wikimedia)

## Mergesort

Merge sort is an efficient comparison-based sorting algorithm. The fundamental operation of merge sort is merging two sorted separated arrays.

The steps of merge sort are:

- Divide the array into two arrays from the middle.
- Repeat the division until each array contains only one element.
- Repeat merging pairs of arrays by comparison of elements.

(Source: Wikipedia and Wikimedia)

## Quicksort

Quick sort is a divide and conquer algorithm. It is quite efficient.

The steps of quick sort are:

- Pick an element called a pivot. Normally, you can pick the first one, the last one, the median one or a random one. But it is a risk to choose the first or the last when the array is pre-ordered.
- Partitioning: reorder the array so that all elements that are less than the pivot come before the pivot and all elements that are larger than the pivot come after the pivot.
- Recursively apply the above steps to the sub-array.

(Source: Wikipedia and Wikimedia)

## Bucket Sort

Bucket sort is a distribution sort that works by putting elements into some buckets.

The steps of bucket sort are:

- Create several empty buckets.
- According to the distribution and range of each buckets, put each element into its corresponded bucket.
- Sort each non-empty bucket using one of the simple sort algorithms.
- Concatenate the buckets in order or put all elements into the original array based on the order of buckets.

(Image source: Wikipedia and Wikimedia)

## Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly compares adjacent pairs and swaps them if they are in wrong order.

(Source: Wikipedia and Wikimedia)

## Selection Sort

The basic idea of selection sort is finding the minimum element repeatedly.

The steps of selection sort are:

- Find the minimum element from the array.
- Move it to the front of the array.
- Find the minimum element from the rest part of the array.
- Move it to the front of the sub-array.
- Repeat step 3 and step 4.

(Source: Wikipedia and Wikimedia)

# Source Code

The sources of implementation for these sorting algorithms can be found in my GitHub.

# External Sorting

Sorting that cannot be performed in main memory and must be done on disk is also important.

There are some external sorting algorithms such as external merge sort(multiway merge, polyphase merge, replacement selection) and external distribution sort.

# References

- “
*Data Structures and Algorithm Analysis in Java*” by Mark A. Weiss. - 《数据结构（C语言）》 by 严蔚敏 吴伟民
- GitHub - ZhuzhuLearning/Algorithm

blog comments powered by Disqus