您的位置:首页 > 其它

直接插入排序 direct insertion sort

2007-01-28 20:12 337 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: