折半插入排序

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

}

}

}

相关推荐
吃好睡好便好7 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
仰泳之鹅7 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
x_yeyue10 小时前
三角形数
笔记·算法·数论·组合数学
Xin_ye1008610 小时前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
rockey62710 小时前
AScript异步执行与await关键字
c#·.net·script·eval·expression·异步执行·动态脚本
念何架构之路11 小时前
Go语言加密算法
数据结构·算法·哈希算法
AI科技星11 小时前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
失去的青春---夕阳下的奔跑11 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
黎阳之光11 小时前
黎阳之光:以视频孪生重构智慧医院信息化,打造高标项目核心竞争力
大数据·人工智能·物联网·算法·数字孪生
丷丩12 小时前
三级缓存下MVT地图瓦片服务性能优化策略
算法·缓存·性能优化·gis·geoai-up