Sorting Algorithms

What is Sorting?

Sorting is an algorithm that arranges the elements of a list in a certain order [either ascending or descending]. The output is a permutation or reordering of the input.

Why is Sorting Necessary?

Sorting is one of the important categories of algorithms in computer science and a lot of research has gone into this category. Sorting can significantly reduce the complexity of a problem, and is often used for database algorithms and searches.

Classification of Sorting Algorithms

Sorting algorithms are generally categorized based on the following parameters.

By Number of Comparisons

In this method, sorting algorithms are classified based on the number of comparisons. For comparison based sorting algorithms, best case behavior is O(nlogn) and worst case behavior is O(n^2). Comparison-based sorting algorithms evaluate the elements of the list by key comparison operation and need at least O(nlogn) comparisons for most inputs.

By Number of Swaps

In this method, sorting algorithms are categorized by the number of swaps (also called inversions).

By Memory Usage

Some sorting algorithms are “in place” and they need O(1) or O(logn) memory to create auxiliary locations for sorting the data temporarily.

By Recursion

Sorting algorithms are either recursive [quick sort] or non-recursive [selection sort, and insertion sort], and there are some algorithms which use both (merge sort).

By Stability

Sorting algorithm is stable if for all indices i and j such that the key A[i] equals key A[j], if record R[i] precedes record R[j] in the original file, record R[i] precedes record R[j] in the sorted list. Few sorting algorithms maintain the relative order of elements with equal keys (equivalent elements retain their relative positions even after sorting).

By Adaptability

With a few sorting algorithms, the complexity changes based on pre-sortedness [quick sort]: presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive.

Other Classifications

Another method of classifying sorting algorithms is:

  • Internal Sort
  • External Sort

Internal Sort

Sort algorithms that use main memory exclusively during the sort are called internal sorting algorithms. This kind of algorithm assumes high-speed random access to all memory.

External Sort

Sorting algorithms that use external memory, such as tape or disk, during the sort come under this category.

Sorting Algorithm Performance

When we measure the performance of a sorting algorithm, there are two operations that we are primarily concerned with:

  1. Comparisons

    • When two values in the array being sorted are compared for relative equality.
  2. Swaps

    • When two values in the array being sorted exchange places.
  3. Comparisons and Swaps are expressed using the Big-O notation.

  4. The actual cost of either operation depends on multiple factors.

    • Comparing two integers will be faster than comparing two strings.
    • Swapping two values that are both in memory will be faster than swapping two values that are stored on disk or another physical media.
  5. Reducing either operation can improve performance.