Sort algorithm Summary (i) Insert sort "insertion sort"

Source: Internet
Author: User

Recently busy looking for work, previously looked at the sorting algorithm have forgotten, tragedy T T now to review it.

It is very useful to recommend an algorithm visualization site here. http://visualgo.net/

I. Idea of insertion sequencing (Wikipedia):

It works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it. The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.

    1. Starting with the first element, the element can be thought to have been sorted
    2. Takes the next element and scans the sequence of elements that have been sorted from back to forward
    3. If the element (sorted) is greater than the new element, move the element to the next position
    4. Repeat step 3 until you find the sorted element is less than or equal to the position of the new element
    5. After inserting a new element into the position
    6. Repeat step 2~5

Tips: If the comparison operation is more expensive than the swap operation , a binary lookup can be used to reduce the number of comparison operations . The algorithm can be thought of as a variant of the insertion sort , called a binary lookup insert sort.

Two: Process

Raw data

As shown, the first number is set to the number of sorted completion, at which point the number of orders that need to be sorted forward is compared, and if the sorted number (11) is greater than the modified element (5), the sorted number is moved back one (11) until it finds a number smaller than its own (5) or reaches the head of the array.

Repeat the process above

Three. Code

#include <iostream> #include <vector>using namespace std;template <typename t>void insertionsort ( Vector<t> &nums) {for (int i = 1; i < nums.size (); i++) {T temp = Nums[i];int J;for (j = i-1; J >= 0 && Amp NUMS[J] > temp; j--) {nums[j+1] = nums[j];//corresponding 3}nums[j+1] = temp; 4.5}}int Main () {vector<int> nums{11,5,29,1,34,4,12,24,40,5,35,17};cout<< "Before Sort:"; for (auto M: Nums) {cout <<  m << "";} Cout<<endl;insertionsort (nums);cout<< "after Sort:", for (auto m:nums) {cout  << m << "";} Cout<<endl;}

Four. Summary

1. First insert sort is a stable sort

2. For the best case, where the original data is sorted, the insertion sort requires only a n-1 comparison operation

3. For the worst case scenario, where the data is in descending order, in which case a total of 1+2+3 is required .... (n-1) is n (n-1)/2 comparison operation and N (n-1)/2 + (n-1) secondary assignment operation, so the total time complexity is O (n2)

4. In C + + STL, insert sort as a complement to a quick order, for ordering of a small number of elements, typically 8 or less.

Sort algorithm Summary (i) Insert sort "insertion sort"

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.