Nine sort specific implementation code

Time:2021-7-22

Statement: the content of this article is only for reference. If there is any mistake, the blogger strongly hopes you to point out. If you are the original blogger of a certain knowledge point, if necessary, you can contact me and add a link. The content of this article will be updated according to the needs of bloggers. I hope you will pay more attention to it.

Direct insertion sort

void InsertSort(int r[])
{
    int n = sizeof(r) / sizeof(r[0]);
    for(int i = 1; i < n; ++i)
    {
        for(int j = i - 1; j >= 0; --j)
        {           
            if(r[j+1] < r[j])
            {
                int s = r[j+1];
                r[j+1] = r[j];
                r[j] = s;
            }
        }
    }
}

Binary Insertion Sort

void BinInsertSort(int r[])
{
    int n = sizeof(r) / sizeof(r[0]);
    for(int i = 1; i < n; ++i)
    {
        int s = r[i];
        int low = 0;
        int high = i - 1;
        
        while(low <= high)
        {
            int mid = (low + high) / 2;  // The mid position is the number to find
            if(s < r[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
        
        for(int j = i - 1;  j >= high + 1; -- j) // high + 1 is mid, the number of execution is moved back until the number of mid is moved back
            r[j+1] = r[j];
        
        r[high+1] = s;  // The mid location stores itself
    }
}

Shell Sort

void ShellSort(int r[])
{
    int n = sizeof(r) / sizeof(r[0]);
    int step = n / 2;
    
    while(step >= 1)
    {
        for(int i = step; i < n; ++i)
        {
            for(int j = i - step; j >= 0; j -= step)
            {                
                if(r[j+step] < r[j])
                {
                    int s = r[j+step];
                    r[j+step] = r[j];
                    r[j] = s;
                }
            }
        }
        step /= 2;
    }
}

Direct selection sort

void SelectSort(int r[])
{
    int n = sizeof(r) / sizeof(r[0]);
    for(int i = 0; i < n - 1; ++i)
    {
        int samll = i;
        for(int j = i + 1; j < n; ++j)
        {
            if(r[small] > r[j])
                samll = j;
        }
        if(small != i)
        {
            int s = r[i];
            r[i] = r[small];
            r[small] = s;
        }
    }
}

Heap sort

void HeapAdjust(int r[];  int i;  Int j) // adjust heap
{
    int child = 2 * i;
    int s = r[i];  // S stores node data temporarily
    while(child <= j)
    {
        If (child < J & & R [child + 1] > R [child]) // compare 2 subtrees
            ++child;
        If (s > = R [child]) // compare node with big subtree
            break;
        r[child/2] = r[child];  // If the big subtree is bigger than the node, it is interchangeable
        child = 2 * child;  // Continue searching to subtree
    }
    r[child/2] = s;  // The number of nodes is the largest
}

Void HEAPSORT (int r [] // build heap
{
    int n = sizeof(r) / sizeof(r[0]);
    for(int i = n / 2 - 1;  i >= 0; -- i) // only subscripts before N / 2-1 have subtrees
    {
        HeapAdjust(r, i, n - 1);  // The nodes are larger than the subtree, and the last root node is the largest number
    }
    for(int i = n - 1; i > 0; --i)
    {
        //Swap the current top element with the current tail element, that is, move the maximum number to the end
        int s = r[0];
        r[0] = r[i];
        r[i] = s;
        HeapAdjust(r, 0, i -1);  // The rest of the elements continue to adjust, and finally become the order from small to large
    }
}

Bubble sort

void BubbleSort(int r[])
{
    int n = sizeof(r) / sizeof(r[0]);
    for(int i = 0; i < n - 1; ++i)
    {
        for(int j = 0; j < n - 1 - i; ++j)
        {
            if(r[j] > r[j+1])
            {
                int s = r[j];
                r[j] = r[j+1];
                r[j+1] = s;
            }
        }
    }
}

Quick sort

int Partition(int r[], int low, int high)
{
    int pivotkey = r[low];
    int i = low;
    int j = high;
    
    while(i < j)
    {
        While (I < J & & R [J] > pivot key) // find the first number smaller than pivot key from J
            --j;
        if(i < j)
        {
            r[i] = r[j];
            ++i;
        }
        
        While (I < J & & R [i] < pivot key) // look from I to find the first number larger than pivot key
            ++i;
        if(i < j)
        {
            r[j] = r[i];
            --j;
        }
    }
    r[j] = pivotkey;  // Complete the final exchange
    return j;  // Return to the dividing point. The front number is smaller than the pivot key, and the back number is larger than the pivot key
}

Void quicksort (int r [] int low, int high) // pass array, 0 and length - 1
{
    if(low < high)
    {
        int pivot = Partition(r, low, high);
        QuickSort(r, low, pivot - 1);  // Recursion, the first half continues with quicksort
        QuickSort(r, pivot + 1;  high);  // Recursion, the second half continues with quicksort
    }
}

Merge sort

void copyArray(int source[], int dest[], int len, int first)
{
    int i;
    int j = first;
    for(i = 0; i < len; ++i)
    {
        dest[j] = source[i];
        ++j;
    }
}

void merge(int a[], int left, int right)
{
    int begin1 = left;
    int mid = (left + right) / 2;
    int begin2 = mid + 1;
    int newArrayLen = right - left + 1;
    int *b = (int*)malloc(newArrayLen * sizeof(int));
    int k = 0;
    
    While (begin1 < = mid & & begin2 < = right) // find out the smaller number in the two groups and put it into space B in order
    {
        if(a[begin1] <= a[begin2])
            b[k++] = a[begin1++];
        else
            b[k++] = a[begin2++];
    }
    
    //Put the rest in space B
    while(begin1 <= mid)
        b[k++] = a[begin1++];
    while(begin2 <= right)
        b[k++] = a[begin2++];
    
    copyArray(b, a, newArrayLen, left);  // Write the number of B space back to the original array
    free(b);
}

Void mergesort (int r [] int left, int right) // pass array, 0 and length - 1
{
    int i;
    //At least two elements are sorted
    if(left < right)
    {
        i = (left + right) / 2;
        MergeSort(r, left, i);  // First half recursion
        MergeSort(r, i + 1, right); // Second half recursion        
        merge(r, left, right);  // The comparison order of 10 numbers is [0,1] [0,2] [3,4] [0,4] [5,6] [5,7] [8,9] [5,9] [0,9]
    }
}

Bucket sorting

void insert(list<int> &bucket, int val)
{
    auto iter = bucket.begin();
    while(iter != bucket.end() && val >= *iter)
        ++iter;
    //Insert will insert data before ITER, which can stabilize sorting
    bucket.insert(iter, val);
}

void BuckdeSort(vector<int> &arr)
{
    int len = arr.size();
    if(len <= 1)
        return;
    int min = arr[0], max = min;
    for(int i = 1;  i < len; ++ i) // find the minimum and maximum
    {
        if(min > arr[i])
            min = arr[i];
        if(max < arr[i])
            max = arr[i];
    }
    
    int k = 10;  // The size of the barrel
    
    //If [0,9] has 10 numbers, then (9-0) / K + 1 = 1 bucket
    int bucketsNum = (max - min) / k + 1;  // Number of barrels
    
    vector<list<int>> buckets(bucketsNum);
    for(int i = 0; i < len; ++i)
    {
        int  value = arr[i];
        //(value min) / K is in which bucket
        insert(buckets[(value-min)/k], value);  // Put the data in each bucket and sort it
    }
    
    int index = 0;
    for(int i = 0; i < bucketsNum; ++i)
    {
        if(buckets[i].size())
        {
            for(auto &value : buckets[i])
                arr[index++] = value;
        }
    }
}

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]