C++ 插入排序 (直接插入/拆半插入/希尔排序)

前置知识

插入排序,一般也被称为直接插入排序。对于少量元素的排序,它是一个有效的算法 。插入排序是一种最简单的排序方法,它的基本思想是将一个记录插入到已经排好序的有序表中,从而一个新的、记录数增1的有序表。在其实现过程使用双层循环,外层循环对除了第一个元素之外的所有元素,内层循环对当前元素前面有序表进行待插入位置查找,并进行移动 。 -----百度百科

实现代码
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
int a[10];
void insertSort(int a[], int len) {//直接插入排序
	for (int i = 1; i < len; i++) {
		if (a[i] < a[i - 1]) {
			int temp = a[i];
			int j = i - 1;
			for (; j >= 0; j--) {
				if (a[j] < temp) break;
				a[j + 1] = a[j];
			}
			a[j + 1] = temp;
		}
	}
}

void binInsertSort(int a[], int len) {//拆半插入排序
	for (int i = 1; i < len; i++) {
		if (a[i] < a[i - 1]) {
			int temp = a[i];
			int  l = 0, r = i - 1;
			while (l<=r)
			{ 
				int mid = l + r >> 1;
				if (a[mid] > temp) r = mid - 1;
				else l = mid+1;
			}
			for (int j = i - 1; j >= l; j--) a[j + 1] = a[j];
			a[r+1] = temp;
		}
	}
}

void shellSort(int a[], int len) {//希尔排序
	int d = len / 2;
	while (d>0) {
		for (int i = d; i < len; i++) {
			int temp = a[i];
			int j = i - d;
			while (j >= 0 && a[j] > temp) {
				a[j + d] = a[j];
				j = j - d;
			}
			a[j + d] = temp;
		}
		d /= 2;
	}
}
int main() {
    //排序成递增序列
	for (int i = 0; i < 10; i++) cin >> a[i];
	insertSort(a,end(a)-begin(a));         //直接插入排序
	for (int i = 0; i < 10; i++) cout << a[i] << " ";
	cout << endl;
	for (int i = 0; i < 10; i++) cin >> a[i];
	binInsertSort(a, end(a) - begin(a));   //拆半插入排序
	for (int i = 0; i < 10; i++) cout << a[i] << " ";
	cout << endl;
	for (int i = 0; i < 10; i++) cin >> a[i];
	shellSort(a, end(a) - begin(a));       //希尔排序
	for (int i = 0; i < 10; i++) cout << a[i] << " ";
	cout << endl;
}
//样例: 5 1 4 2 6 9 3 8 7 0
此为用C#写的A*算法源代码 using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace EtSoft.AStarLib { public class AStar { private StarNodeCollection openList = new StarNodeCollection(); private StarNodeCollection closeList = new StarNodeCollection(); public StarNode currentNode = null; #region 构造函数 /// <summary> /// 使用指定的地图对象、起点和终点初始化A星算法 /// </summary> /// <param name="map">地图对象</param> public AStar(Map map) { this.map = map; } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="start">起点坐标</param> /// <param name="end">终点坐标</param> public AStar(Map map, Point start, Point end) : this(map) { this.start = new StarNode(start); this.end = new StarNode(end); openList.Add(new StarNode(start)); //AddStartNodeToOpenList(this.start); } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="startX">起点X坐标</param> /// <param name="startY">起点Y坐标</param> /// <param name="endX">终点X坐标</param> /// <param name="endY">终点Y坐标</param> public AStar(Map map, int startX, int startY, int endX, int endY) : this(map, new Point(startX, startY), new Point(endX, endY)) { } #endregion #region 属性 protected Map map; /// <summary> /// 地图数据 /// </summary> public Map Map { get { return map; } set { map = value; } } private StarNode start = null; /// <summary> /// 起点坐标,如果没有设置起点,返回null /// </summary> public StarNode Start { get { return start; } set { start = value; openList.Clear(); openList.Add(start); //AddNodeToOpenList(start); } } private StarNode end = null; /// <summary> /// 终点坐标,如果没有设置终点,返回null /// </summary> public StarNode End { get { return end; } set { end = value; } } private StarNodeCollection path; /// <summary> /// 返回路径节点集合,没有路径则返回null /// </summary> public StarNodeCollection Path { get { return path; } } private bool allDirection = false; /// <summary> /// true,允许八方向寻路 /// </summary> public bool AllDirection { get { return allDirection; } set { allDirection = value; } } #endregion /// <summary> /// 开始寻路 /// </summary> public void StartSearchPath() { if (start == null) throw new InvalidNodeException(InvalidNodeTypes.NoStart); if (end == null) throw new InvalidNodeException(InvalidNodeTypes.NoEnd); path = null; openList.Clear(); closeList.Clear(); currentNode = start; SearchPath(currentNode); } //寻路递归,受保护的虚方法,允许在子类重写寻路算法 protected virtual void SearchPath(StarNode starNode) { //currentNode = starNode; openList.Remove(starNode); closeList.Add(starNode); AddNodeToOpenList(); if (closeList.Contains(end)) { //如果终点在关闭列表中,找到路径 StarNode node=starNode.Parent; path = new StarNodeCollection(); do { path.Add(node); node = node.Parent; } while (node != null && !node.Equals(start)); path.Reverse(); return; } else if (openList.Count == 0) { //终点不在关闭列表,开放列表已空,没有可通行的路径 return; } currentNode = GetNextNode(); //迭代过程 SearchPath(currentNode); } /// <summary> /// 获得当前节点的下一个最佳节点 /// </summary> /// <returns></returns> public StarNode GetNextNode() { openList.SortByF(); return openList[0]; } /// <summary> /// 把当前点周围可通过的点加入到开放列表中 /// </summary>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值