sailing

Everything...

直接插入排序 direct insertion sort

#include "stdafx.h"

#include <iostream>

#include "stdio.h"

 

void DirectInsertSort(int a[], int iLen);

void PrintArray(int a[], int iLen);

 

int _tmain(int argc, _TCHAR* argv[])

{

    // the first element of the array is reserved for use in the algorithm

    int arrayToSort[] = {0, 7, 3, 4, 25, 15, 29, 12, 4, 1};

    int n = sizeof(arrayToSort)/sizeof(int);

    PrintArray(arrayToSort, n);

    DirectInsertSort(arrayToSort, n);

    PrintArray(arrayToSort, n);

    return 0;

}

 

void DirectInsertSort(int a[], int iLen)

{

    if(a==NULL)

    {

       std::cerr<<"array shouldn't be null";

       return;

    }

 

    for(int i=2;i<iLen;i++)

    {

       a[0] = a[i];

       for(int j=i-1;j>=0;j--)

       {

           if(a[0]<a[j])

           {

              a[j+1]=a[j];

           }

           else

           {

              a[j+1]=a[0];

              break;

           }

       }

    }

}

 

void PrintArray(int a[], int iLen)

{

    for(int i=1; i< iLen; i++)

    {

       std::cout<<a[i]<<' ';

    }

    std::cout<<std::endl;

}

 

 

posted on 2007-01-28 20:12  乌生鱼汤  阅读(183)  评论(0编辑  收藏  举报

导航