Common Sorting Algorithms

Keywords: C++

Counting order:

1. A non-comparison-based sorting algorithm, proposed by Harold H. Seward in 1954, has the advantage of sorting integers in a certain range.

Its time complexity is(n+k), where k is the range of integers, faster than any comparative sorting algorithm.

2. Steps:

(a) Find the maximum Max and minimum min of a given sequence of integers and create an auxiliary space of size max-min+1 with a value of 0

b. The lowest number is 0, the maximum number is - 1, and the other num is num-min.

c. Add one at the corresponding subscript according to the number found by traversal

d. Traverse the auxiliary space and output the number of corresponding subscripts. The number of times the output is the value of the subscripts. That is, if the auxiliary space temp [0]= 5, the number of times the output subscript 0 corresponds to is five times.

Note: This algorithm is based on space for time. If the column to be sorted is not an integer, it is better not to use it, because the floating point range is relatively large, even in [0, 1] this interval can have many.

#include <iostream>
#include <vector>
#include <algorithm>

void countSort(std::vector<int>& nums);

int main()
{
    std::vector<int> nums;
    int len = 0;
    std::cout<<"Please enter the length:";
    do {
        std::cin>>len;
        if (len <= 0)
            std::cerr<<"Please enter a positive integer:";
    } while (len <= 0);
    int num = 0;
    std::cout<<"input "<<len<<" Number: ";
    for (size_t i = 0; i < len; ++i) {
        std::cin>>num;
        nums.push_back(num);
    }
    std::cout<<"The sorted array:";
    countSort(nums);
    
    system("pause");
    
    return 0;
}

void countSort(std::vector<int>& nums)
{
//    Be careful to use '*' Number, because they all find locations, and both functions are contained in algorithm inside 
    int max = *max_element(nums.begin(), nums.end());
    int min = *min_element(nums.begin(), nums.end());
    int size = max - min + 1;
//    Create auxiliary space, initialize to 0 
    std::vector<int> temp(size, 0);
//    Increase the number of subscriptions by one 
    for (auto num : nums)
        temp[num-min] += 1; 
    for (int i = 0; i < size; ++i) {
        int count = temp[i];
//        According to the number of outputs, we have not changed the order of the original sequence here.
//        Only output the sorted sequence. To change the original sequence, you need to perform a copy operation. 
        while (count > 0) {
            std::cout<<i+min<<std::ends;
            count--;
        }
    }
    std::cout<<std::endl;
}

 

Cardinal sorting:

1. Cardinal sorting is a non-comparative sort based on the bits of the elements of the column to be sorted; it can be divided into two categories:

Minimum-bit-first method, LSD method for short: first sort from the lowest bit, then sort from the lowest bit, until the highest bit is sorted and an ordered sequence is obtained.

The highest-order-first method, referred to as MSD method, starts from the highest-order, then subordinates each group according to the next-highest-order, and circulates until the lowest-order.

The following is a dynamic demonstration of LSD

2. Step (LSD):

(a) Determine the number of cycles based on the maximum number of digits of the elements to be sorted, such as 12345 for the maximum number of digits, five cycles

b. Create 10 empty tables representing 0-9, respectively

c. Starting from the lowest level, select tables based on the number of bits. If 123 is 3 at the lowest level, place it in tables marked 3 below.

d. Take out the elements in the table in order to replace the number of the original sequence.

e. Move one bit higher and repeat c and d until the number of cycles

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib> 

int get_times(int num);
int get_digit(int num, int d);
void radixSort(std::vector<int>& nums);

int main()
{
    std::vector<int> nums;
    int len = 0;
    std::cout<<"Please enter the length:";
    do {
        std::cin>>len;
        if (len <= 0)
            std::cerr<<"Please enter a positive integer:";
    } while (len <= 0);
    int num = 0;
    std::cout<<"input "<<len<<" Number: ";
    for (size_t i = 0; i < len; ++i) {
        std::cin>>num;
        nums.push_back(num);
    }
    radixSort(nums);
    std::cout<<"The sorted array:";
    for (int num : nums)
        std::cout<<num<<std::ends;
    std::cout<<std::endl;
    
    system("pause");
    
    return 0;
}

// Get the first d Bit value
int get_digit(int num, int d)
{
    return int(num / pow(10, d)) % 10;
}

// Get the number of cycles required
int get_times(int num)
{
    int times = 0;
    while (num) {
        times++;
        num /= 10;
    }
    return times;
}

//Without considering negative numbers, you can use two arrays, one for positive numbers and one for negative numbers, if necessary. 
void radixSort(std::vector<int>& nums)
{
    int max = *max_element(nums.begin(), nums.end());
    int times = get_times(max);
    int len = nums.size();
    
    for (size_t i = 0; i < times; ++i) {
        std::vector<std::vector<int>> temp(10);
        for (int num : nums) {
            temp[get_digit(num, i)].push_back(num);
        }
                // Clear Array Content
        nums.clear();
                // assignment
        for (auto vec : temp) {
            for (int num : vec) {
                nums.push_back(num);
            }
        }
    }
    return ;
}

 

Bucket sorting:

1. Scanning chooses the maximum Max and minimum min of the column to be sorted. With K barrels, we divide the interval [min, max] into k intervals evenly.

Each interval is a bucket, and then the elements to be sorted are allocated to their respective buckets, i.e. the size of the values is allocated in which interval.

2. To sort the elements in each bucket, you can choose any algorithm.

3. Merge the elements in each bucket into a large ordered sequence

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>

void bucketSort(std::vector<int>& num);

int main()
{
    std::vector<int> nums;
    int len = 0;
    std::cout<<"Please enter the length:";
    do {
        std::cin>>len;
        if (len <= 0)
            std::cerr<<"Please enter a positive integer:";
    } while (len <= 0);
    int num = 0;
    std::cout<<"input "<<len<<" Number: ";
    for (size_t i = 0; i < len; ++i) {
        std::cin>>num;
        nums.push_back(num);
    }
    
    std::cout<<"The sorted array:";
    bucketSort(nums);
    
    system("pause");
    
    return 0;
}

//I didn't change the order of the original sequence, I just output the ordered sequence. To change the original sequence, add an assignment operation. 
void bucketSort(std::vector<int>& nums)
{
//    Finding the Maximum and Minimum 
    int max = *max_element(nums.begin(), nums.end());
    int min = *min_element(nums.begin(), nums.end());
    
//    For convenience, the number of barrels is directly determined by the range size. 
    int buckets_num = max-min+1;
    std::vector<std::vector<int>> res(buckets_num);
    
    for (int num : nums) {
//        Press in 
        res[num-min].push_back(num);
    }
    
    for (auto vec : res) {
//        Bucket sorting 
        sort(vec.begin(), vec.end());
    }
        
    for (auto vec : res)
        for (int num : vec)
            std::cout<<num<<std::ends;
    std::cout<<std::endl;
        
    return ;
}

Posted by arie_parie on Sat, 11 May 2019 15:12:52 -0700