b/C + + sort bis (direct insertion sort, binary insertion sort, merge sort (recursive))

Source: Internet
Author: User

Direct insertion Sort, binary insertion sort, merge sort

4, Direct insertion sort Insertion_sort

#include <iostream>

void insertion_sort (int *begin, int *end)
{
    int *i, *j, temp;
    for (i = begin + 1; I <= end; i + +)
    {
        temp = *i;
        for (j = i-1 J >= begin; J-)
        {
            if (temp < *J)
                * (j + 1) = *j;
            else break
                ;
        }
        * (j + 1) = temp;
    }
int main ()
{
    int data[] = {1, 3, 5, 8, 0, 2, 7};
    Insertion_sort (data + 0, data + 8);
    for (int *i = data; I <= data + 8; i + +)
    {
        std::cout << *i << "";
    }
    Std::cout << Std::endl;
    return 0;
}

5, binary insertion sort Binary_insertion_sort

#include <iostream>

void binary_insertion_sort (int *begin, int *end)
{
    int *i, *j, *low, *high;
    for (i = begin + 1; I <= i + +)
    {
        int temp = *i;//temporary storage
        /Find insertion position for
        (low = begin, High = i-1; <= High;
        {
            int *middle = low + (high-low)/2;
            if (temp < *middle) high
                = middle-1;
            else low
                = middle + 1;
        }
        Move backward for
        (j = i-1 J >= High + 1; J-)
            * (j + 1) = *j;
        Insert
        * (j + 1) = temp;
    }
int main ()
{
    int data[] = {1, 3, 5, 8, 2, 7};
    Binary_insertion_sort (data + 0, data + 8);
    for (int *i = data; I <= data + 8; i + +)
    {
        std::cout << *i << "";
    }
    Std::cout << Std::endl;
    return 0;
}

6. Merge sort (recursive algorithm) merge_sort

#include <iostream>//merge two ordered tables into an ordered table void merge (int *begin, int *mid, int *end, int *temp) {int *l_begin = Beg
    In;
    int *l_end = mid;
    int *r_begin = mid + 1;
    int *r_end = end;
    int i = 0; while (L_begin <= l_end && r_begin <= r_end) {if (*l_begin > *r_begin) {t
            Emp[i] = *r_begin;
            i + +;
        R_begin + +;
            else {temp[i] = *l_begin;
            i + +;
        L_begin + +;
        } while (L_begin <= l_end) {temp[i] = *l_begin;
        i + +;
    L_begin + +;
        while (R_begin <= r_end) {temp[i] = *r_begin;
        i + +;
    R_begin + +;
        while (begin <= end) {*begin = *temp;
        Begin + +;
    temp + +; }//void merge_sort_recursive (int *begin, int *end, int *temp) {if (End-begin > 0) {int *mid = b
        Egin + (End-begin)/2; Merge_sort_rEcursive (begin, Mid, temp);
        Merge_sort_recursive (mid + 1, end, temp);
    Merge (begin, Mid, end, temp); }//void merge_sort (int * begin, int * end) {if (End-begin > 0) {int * temp = new Int[end-begi

        n + 1];

        Merge_sort_recursive (begin, end, temp);
    delete [] temp;

    int main () {int data[] = {7, 8, 5, 12, 3, 2, 1, 0, 10};

    Merge_sort (data + 0, data + 8);
    for (int i = 0; i < 8; + + i) {std::cout << data[i] << "";
    } std::cout << Std::endl;
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.