# 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

- Find the smallest element in the array
- Swap it with the first element of the array
- Find the smallest of the remaining elements
- Swap it with the second element in the array
- 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:

- 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.
- 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:

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

Select the difference between sorting and insert sorting:

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