Inserting a sort C + + implementation

Source: Internet
Author: User

⒈ starting with the first element, the element can be thought to have been sorted
⒉ takes the next element and scans the sequence of elements that have been sorted from backward forward
⒊ if the element (sorted) is greater than the new element, move the element to the next position
⒋ Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element
⒌ inserting new elements into the next position
⒍ Repeat steps 2~5
#include <iostream>
#include <array>
using namespace Std;


Template<class t>
void Insertion_sort (t&, int);


int main ()
{
Array<int, 10> arr={1,3,2,5,4,6,8,7,9,0};
Insertion_sort (arr, arr.size ()); Input arrays and array sizes, because array is a class, so we cannot take arr as the array header, we refer directly to the array class
for (int i = 0; i <; i++)
{
cout << Arr[i] << Endl;
}


Cin.get ();
return 0;
}


Template<class t>
void Insertion_sort (t& arr, int count)
{
int p = 0;
for (int i = 1; i < count; i++)
{
Auto number = Arr[i];
for (p = i; p > 0 && number < arr[p-1]; p--)
{
ARR[P] = arr[p-1];
}
ARR[P] = number;
}
}

Inserting a sort C + + implementation

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.