Sorting

In data structures, sorting refers to the process of rearranging a collection of elements (such as numbers, strings, or objects) into a specific order, typically ascending or descending. This is achieved using sorting algorithms, which are designed to efficiently organize data for better analysis, retrieval, and manipulation.

Common sorting algorithms:

  • Bubble Sort: Simple but inefficient for large datasets. Repeatedly compares adjacent elements and swaps them if they are in the wrong order.

  • Insertion Sort: Efficient for small or partially sorted datasets. Iterates through the list, inserting each element into its correct position in the sorted part of the list.

  • Selection Sort: Divides the list into sorted and unsorted parts. Repeatedly finds the smallest element in the unsorted part and swaps it with the first element of the unsorted part.

  • Quick Sort: Divide and conquer algorithm that is generally efficient for large datasets. It works by recursively partitioning the list into smaller sublists and sorting them independently.

  • Merge Sort: Another divide and conquer algorithm that is efficient for large datasets. It works by recursively dividing the list into halves, sorting them independently, and then merging them back together in sorted order.

  • Heap Sort: Efficient algorithm that uses a heap data structure to achieve O(n log n) time complexity in all cases. It's in-place, stable, and versatile, also used for priority queues.

Example

    public static void InsertionSort(int[] arr)
    {
        int n = arr.Length;

        // Iterate through the array, starting from the second element
        for (int i = 1; i < n; i++)
        {
            int key = arr[i]; // Store the current element to be inserted
            int j = i - 1;

            // Shift elements greater than the key to the right
            while (j >= 0 && arr[j] > key)
            {
                arr[j + 1] = arr[j];
                j--;
            }

            // Insert the key in its correct position
            arr[j + 1] = key;
        }
    }

Complexity

  • Time complexity: O(N^2)

  • Space complexity: O(1)

Factors to consider when choosing a sorting algorithm:

  • Time complexity: How long the algorithm takes to run in terms of the number of elements to be sorted.

  • Space complexity: How much extra memory the algorithm requires during its execution.

  • Stability: Whether the algorithm preserves the relative order of elements with equal keys.

  • Nature of the input data: Some algorithms are more efficient for certain types of data (e.g., partially sorted data).

Complexity Table

AlgorithmBest-case Time ComplexityAverage-case Time ComplexityWorst-case Time ComplexitySpace ComplexityStability

Bubble Sort

O(n)

O(n^2)

O(n^2)

O(1)

Stable

Insertion Sort

O(n)

O(n^2)

O(n^2)

O(1)

Stable

Selection Sort

O(n^2)

O(n^2)

O(n^2)

O(1)

Unstable

Merge Sort

O(n log n)

O(n log n)

O(n log n)

O(n)

Stable

Quick Sort

O(n log n)

O(n log n)

O(n^2)

O(log n)

Unstable

Heap Sort

O(n log n)

O(n log n)

O(n log n)

O(1)

Stable

Last updated