折半插入排序

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

}

}

}

相关推荐
王哈哈^_^4 分钟前
【数据集+完整源码】水稻病害数据集,yolov8水稻病害检测数据集 6715 张,目标检测水稻识别算法实战训推教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
light_in_hand19 分钟前
内存区域划分——垃圾回收
java·jvm·算法
Jackson@ML31 分钟前
用Visual Studio Code最新版开发C#应用程序
ide·vscode·c#
小安同学iter1 小时前
SQL50+Hot100系列(11.7)
java·算法·leetcode·hot100·sql50
_dindong1 小时前
笔试强训:Week-4
数据结构·c++·笔记·学习·算法·哈希算法·散列表
星释1 小时前
Rust 练习册 :Nucleotide Codons与生物信息学
开发语言·算法·rust
她说彩礼65万2 小时前
C# 代理模式
开发语言·c#·代理模式
寂静山林2 小时前
UVa 1366 Martian Mining
算法
陌路202 小时前
S12 简单排序算法--冒泡 选择 直接插入 希尔排序
数据结构·算法·排序算法
雾岛—听风3 小时前
P1012 [NOIP 1998 提高组] 拼数
算法