C + + insertion sorting algorithm

Source: Internet
Author: User

First, the algorithm description

Inline sequencing It's easy to understand that when we play poker, every time we touch the card, it will be inserted into the appropriate position by the number or suit, until we finish the last card, and the cards in our hands are arranged in order of size. This whole process is an insert sort

Here's an example of the initial array {12, 15, 9, 20, 6, 31, 24}

We use the first element 12 as an array of ordinal numbers, enclosed in brackets, to allow us to observe

Then the initial array: [12], 15, 9, 20, 6, 31, 24

The first step, with 15 and 12 comparisons, found that 15 is greater than 12, inserting 15 behind 12, so the current array becomes

Array results: [12, 15], 9, 20, 6, 31, 24

The second step, take the 9 and 15 comparison, found 15>9, move 15 to the back of a bit, in the comparison of 9 and 12 found 12>9, move 12 backward one, and finally insert 9

Array results: [9, 12, 15], 20, 6, 31, 24

The third step, holding 20 and 15 comparisons, found 15<20, no need to adjust

Array results: [9, 12, 15, 20], 6, 31, 24

So execution goes on until all sorts are done,

The above picture can perfectly show the whole sort process, where the dashed line represents the loop execution in the while, see the algorithm implementation

Second, the algorithm implementation

#include "stdafx.h" #include <iostream>using namespace std;void insertsort (int a[], int n) {for (int j = 1; j < N; j + +) {int key = A[j];//To sort the first element int i = j-1;  Represents the number of elements that have been ordered in the last index while (i >= 0 && key < A[i]) {//From backward forward by comparison has been sorted by several groups, if smaller than it, then the latter is replaced by the former,//In fact, the array is moved one after another, To find the right position, it is convenient to insert the key a[i + 1] = a[i];i--;} A[i + 1] = key;//Find a suitable position, assign a value, set the key value after the I index. }}void Main () {int d[] = {9, 9, 6, 6,};cout << "input array {[],", "+"  << Endl ; Insertsort (d,7); cout << "sorted result:"; for (int i = 0; i < 7; i++) {cout << d[i]<< "";}}

  

Third, the test implementation

I hope my article will be of some help to you. If you like to give me a recommendation, thank you ~

C + + insertion sorting algorithm

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.