c++插入排序

直接插入排序:

#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif // WIN32
#include<iostream>
using namespace std;

int main()
{
    int a[10] = { 12,88,72,45,3,15,7,66,92,31 };
    for (int watch = 1; watch < 10; watch++)//watch point 右移;
    {
        for (int end = watch; end > 0; end--)//watch point的前段;
        {
            if (a[end-1]>a[end]) //前段排序。從小到大。
            {
                int temp = a[end-1];
                a[end-1] = a[end];
                a[end] =temp;
            }
        }
    }

    //已排序
    for (auto const& item : a)
    {
        cout << item << endl;
    }
    return 0;
}

原理:

這裏寫圖片描述

發佈了39 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章