[sort] select sort and insert sort

Posted by napier_matt on Thu, 24 Feb 2022 17:49:36 +0100

Sorting algorithm related interface

Sorting focuses on array elements (random access is supported). Each element has a key value. The key value can be arranged (i.e. sorted) in some way.

A sorting algorithm needs to implement the following interfaces:

class SortFunc {
public:
    void sort(Comparables a);

private:
    bool less(const Comparable &a, const Comparable &b);
    void exch(Comparable &a, Comparable &b)
    {
        Comparable t = a;
        a = b;
        b = t;
    }
};

Comparable is the type of a single array element, and comparable is the type of a collection of elements that supports random access.

To complete sort, you need to provide the method less to compare two elements and the method exch to exchange elements according to the comparison results.

When analyzing a sorting algorithm, in addition to the number of calls to less and exch, we also need to pay attention to whether additional memory space is configured to store the copy of the array.

Select sort

  1. Find the smallest element in the array
  2. Swap it with the first element of the array
  3. Find the smallest of the remaining elements
  4. Swap it with the second element in the array
  5. So back and forth until there is only one element left

This algorithm is called selective sorting because it constantly selects the smallest of the remaining elements.

class Solution {
public:
    vector<int> sortArray(vector<int>& nums) 
    {
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            int minIdx = i;
            for (int j = i + 1; j < n; ++j) {
                if (less(nums, j, minIdx)) {
                    minIdx = j;
                }
            }
            exch(nums, i, minIdx);
        }

        return nums;
    }

private:
    bool less(const vector<int>& nums, int i, int j) 
    {
        return nums[i] < nums[j];
    }

    void exch(vector<int>& nums, int i, int j)
    {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
};

Selective sorting has two distinct characteristics:

  1. Run time is independent of input. An ordered array (or an array with all key s equal) takes as long to sort as an array with random elements.
  2. Element moves less. The number of element exchanges is linear with the size of the array.

Insert sort

When arranging bridge cards, insert each card into the appropriate position of other ordered cards.

In the computer implementation, in order to make room for the elements to be inserted, we need to move all other elements to the right one bit before insertion. This algorithm is called insertion sorting.

class Solution {
public:
    vector<int> sortArray(vector<int>& nums) 
    {
        int n = nums.size();
        for (int i = 1; i < n; ++i) {
            int j = i;
            while (j >= 1 && less(nums, j, j - 1)) {
                exch(nums, j, j - 1);
                --j;
            }
        }

        return nums;
    }

private:
    bool less(const vector<int>& nums, int i, int j) 
    {
        return nums[i] < nums[j];
    }

    void exch(vector<int>& nums, int i, int j)
    {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
};

Optimized version: move the larger elements to the right in the inner loop instead of always exchanging elements, so that the number of accesses to the array can be halved.

class Solution {
public:
    vector<int> sortArray(vector<int>& nums) 
    {
        int n = nums.size();
        for (int i = 1; i < n; ++i) {
            int tmp = nums[i];
            int j = i;
            while (j >= 1 && less(tmp, nums[j - 1])) {
                nums[j] = nums[j - 1];
                --j;
            }
            nums[j] = tmp;
        }

        return nums;
    }

private:
    bool less(int i, int j) 
    {
        return i < j;
    }

    void exch(vector<int>& nums, int i, int j)
    {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
};

During each inner loop, keep a copy of num [i], and use this copy for comparison and assignment operations.

Select what sort and insert sort have in common:

  1. All elements to the left of the current index are ordered.
  2. No additional space consumption. O(1) space complexity, O(n^2) time complexity.

Select the difference between sorting and insert sorting:

  1. In selection sorting, each cycle determines the final position of an element. But insert sort does not.
  2. The time required to insert a sort depends on the initial order of the input elements. But selecting sorting does not.

Topics: Algorithm