折半插入排序

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();

}

}

}

相关推荐
爱的叹息39 分钟前
关于 梯度下降算法、线性回归模型、梯度下降训练线性回归、线性回归的其他训练算法 以及 回归模型分类 的详细说明
人工智能·算法·回归·线性回归
佟格湾1 小时前
聊透多线程编程-线程互斥与同步-12. C# Monitor类实现线程互斥
c#·多线程
冠位观测者1 小时前
【Leetcode 每日一题】781. 森林中的兔子
数据结构·算法·leetcode
被AI抢饭碗的人1 小时前
算法题(130):激光炸弹
数据结构·算法
muyouking112 小时前
Unreal Engine中FRotator与FQuat在赛车游戏方向盘控制中的协同应用解析
算法·游戏·虚幻
慕容青峰3 小时前
【蓝桥杯 2025 省 A 扫地机器人】题解
c++·算法·蓝桥杯·sublime text
_yingty_3 小时前
GO语言入门:常用数学函数2
java·学习·算法·golang
时光追逐者3 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 35 期(2025年4.14-4.20)
c#·.net·.netcore
猎猎长风3 小时前
【数据结构和算法】3. 排序算法
数据结构·算法·排序算法
bookish_2010_prj4 小时前
链式栈和线性栈
数据结构·c++·算法