直接插入排序 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;
}
#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;
}
相关文章推荐
- 插入排序:直接插入排序-Direct insertion sort
- 直接插入排序(Insertion Sort)
- 直接插入排序(Insertion Sort)
- 算法系列-------直接插入排序(InsertionSort)
- 直接插入排序(Insertion Sort)
- 插入排序 直接插入排序的PHP实现 Straight Insertion Sort
- 直接插入排序(Insertion Sort)
- 1、直接插入排序(InsertionSort)-C#实现
- 直接插入排序(Straight Insertion Sort)
- 【lintcode】——链表插入排序-Insertion Sort List
- PHP实现排序算法----直接插入排序(Straight Insertion Sort)
- 八大排序算法_直接插入排序(Straight Insertion Sort)
- 简单插入排序(Insertion Sort)——插入类排序法(Java实现)
- 1、直接插入排序(Straight Insertion Sort)
- 算法不会,尚能饭否之排序——直接插入排序(Insert sort)
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insert Sort)
- LeetCode-Insertion Sort List-链表插入排序-链表操作
- 插入排序—直接插入排序(Straight Insertion Sort)