折半插入排序

using System;

using System.Collections;

using System.Runtime.CompilerServices;

namespace HelloWorldApplication

{

struct KeyType

{

public int key;

};

struct SqList

{

public KeyType[] r;

public int length;

};

class HelloWorld

{

static int maxSize = 100;

private static void BInsertSort(SqList l)

{

KeyType temp;

Console.WriteLine("排序前:l.key:{0}, l.length:{1}", StrSqList(l), l.length);

for (int i = 1; i < l.length; i++)

{

temp = l.r[i];

int low = 0;

int high = i - 1;

while (low <= high)

{

int m = (low + high) / 2;

if (temp.key < l.r[m].key)

{

high--;

} else

{

low++;

}

}

for (int j = i - 1; j >= high + 1; j--)

{

l.r[j + 1].key = l.r[j].key;

}

l.r[high + 1].key = temp.key;

// Console.WriteLine("排序中:l.key:{0}, l.length:{1}", StrSqList(l), l.length);

}

Console.WriteLine("排序后:l.key:{0}, l.length:{1}", StrSqList(l), l.length);

}

private static string StrSqList(SqList l)

{

int []a = new int[l.length];

for(int i=0; i < l.length; i++)

{

a[i] = l.r[i].key;

}

return string.Join(",", a);

}

static void Main(string[] args)

{

/* 我的第一个 C# 程序*/

// 初始化线性表

SqList l;

l.r = new KeyType[maxSize];

int []r = new int[]{49,38,65,97,76,13,27,49 };

KeyType key;

for(int i = 0; i < r.Length; i++)

{

key.key = r[i];

l.r[i] = key;

}

l.length = r.Length;

// 调用排序算法

BInsertSort(l);

Console.ReadKey();

}

}

}

相关推荐
Omics Pro1 分钟前
端到端单细胞空间组学数据分析
大数据·数据库·人工智能·算法·数据挖掘·数据分析·aigc
迈巴赫车主1 分钟前
错位排序算法
开发语言·数据结构·算法·排序算法
炽烈小老头4 分钟前
【每日天学习一点算法 2026/03/31】不同路径
学习·算法
Darkwanderor4 分钟前
搜索优化——迭代加深dfs
c++·算法·深度优先·迭代加深
计算机安禾15 分钟前
【数据结构与算法】第17篇:串(String)的高级模式匹配:KMP算法
c语言·数据结构·学习·算法·visual studio code·visual studio·myeclipse
大萌神Nagato18 分钟前
力扣HOT100 Q146LRU缓存
算法·leetcode·缓存
源码之家23 分钟前
大数据毕业设计汽车推荐系统 Django框架 可视化 协同过滤算法 数据分析 大数据 机器学习(建议收藏)✅
大数据·python·算法·django·汽车·课程设计·美食
nianniannnn26 分钟前
力扣 3.无重复字符的最长子串
c++·算法·leetcode
IT大师兄吖40 分钟前
flux-2-Klein-BFS-换头换脸工作流 懒人整合包
算法·宽度优先
波哥学开发1 小时前
深入解析 BEV 图像色彩调整与伪彩色映射:从直方图统计到着色器实现
算法·图形学