插入排序

来源:互联网 发布:visio 网络图库 编辑:程序博客网 时间:2024/06/03 15:29
#include "stdafx.h"void PrintFunc(int a[], int n){    for (int i = 0; i < n;i++)    {        printf("%d ", a[i]);    }    printf("\n");}//插入排序int InsertSort(int a[], int n){    for (int i = 1; i < n; i++)    {        if (a[i]<a[i-1])        {            int j = i - 1;            int x = a[i];            a[i] = a[i-1];            while (j>=0&& a[j]>x)//重点            {                a[j + 1] = a[j];                j--;            }            a[j+1] = x;        }        PrintFunc(a, n);    }    return 0;}int _tmain(int argc, _TCHAR* argv[]){    int a[] = {9,8,7,6,5,4,3,2,1, 0};        InsertSort(a, 10);    //QSort(a, 10);    //PrintFunc(a, 10);    return 0;}

 

 

Out:

E:\Debug>AlgoTest.exe
8 9 7 6 5 4 3 2 1 0
7 8 9 6 5 4 3 2 1 0
6 7 8 9 5 4 3 2 1 0
5 6 7 8 9 4 3 2 1 0
4 5 6 7 8 9 3 2 1 0
3 4 5 6 7 8 9 2 1 0
2 3 4 5 6 7 8 9 1 0
1 2 3 4 5 6 7 8 9 0
0 1 2 3 4 5 6 7 8 9

0 0
原创粉丝点击