折半插入排序

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

}

}

}

相关推荐
AndrewHZ16 分钟前
【图像处理基石】 怎么让图片变成波普风?
图像处理·算法·计算机视觉·风格迁移·cv
无极小卒19 分钟前
如何在三维空间中生成任意方向的矩形内部点位坐标
开发语言·算法·c#
FMRbpm22 分钟前
链表中出现的问题
数据结构·c++·算法·链表·新手入门
Kuo-Teng40 分钟前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
庸子2 小时前
Kubernetes调度器深度解析:从资源分配到亲和性策略的架构师之路
java·算法·云原生·贪心算法·kubernetes·devops
the白勺2 小时前
RabbitMQ-基础-总结
开发语言·c#
Sunhen_Qiletian2 小时前
YOLOv2算法详解(上篇):从经典到进化的目标检测之路
算法·yolo·目标检测
QTreeY1232 小时前
detr目标检测+deepsort/strongsort/bytetrack/botsort算法的多目标跟踪实现
人工智能·算法·yolo·目标检测·计算机视觉·目标跟踪
AiXed2 小时前
PC微信协议之nid算法
python·网络协议·算法·微信
谈笑也风生3 小时前
经典算法题之子集(四)
算法