插入排序
2006-10-12 17:49
141 查看
// insert.cpp : Defines the entry point for the console application.
//当只有一个元素时,元素是有序的。然后插入,比较大小,大者在后。
#include "stdafx.h"
//第一种方法:
template<class T>
void Insert(T a[], int n, const T& x)
...{
for(int i = n-1; i >= 0 && x < a[i]; i--)
a[i+1] = a[i];
a[i+1] = x;
}
template<class T>
void InsertionSort(T a[], int n)
...{
for(int i = 1; i < n; i++)
...{
T t= a[i];
Insert(a, i, t);
}
}
//第二种方法:
template <class T>
void InsertionSort(T a[], int n)
...{
for(int i = 1; i < n; i++)
...{
T t = a[i];
for(j = i -1; j > 0 && t < a[j]; j--)
a[j+1] = a[j];
a[j+1] = t;
}
}
int main(int argc, char* argv[])
...{
return 0;
}
相关文章推荐
- 直接插入排序
- 八大排序算法总结 1-直接插入排序
- 直接插入排序(带哨兵和不带哨兵)
- 比较 N个元素 一个一个地插入到一个堆中 以线性时间建立一个堆 已排序、反序、随机输入的运行时间
- 排序并插入
- 直接插入排序
- 排序算法(一):直接插入排序
- 排序算法汇总(选择排序 ,直接插入排序,冒泡排序,希尔排序,快速排序...)
- 插入排序
- 直接插入排序
- 插入排序 —— 希尔排序
- 常见的五类排序算法图解和实现(插入类:直接插入排序,折半插入排序,希尔排序)
- 排序 2 - 插入排序
- 直接插入排序(哨兵和越界)
- 插入排序的简单实现
- 插入排序 快速排序 分析整理
- 插入排序之希尔排序
- 数据结构之内部排序--直接插入排序
- 插入排序的性能测试对比(C与C++实现)
- 数据结构-直接插入排序