#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;

}

 

 

相关文章:

  • 2021-07-04
  • 2021-04-25
  • 2021-03-31
  • 2021-11-16
  • 2021-09-05
猜你喜欢
  • 2022-03-05
  • 2021-04-04
  • 2021-09-22
  • 2021-05-08
  • 2022-12-23
  • 2021-12-30
  • 2021-08-30
相关资源
相似解决方案