折半插入排序

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

}

}

}

相关推荐
hh随便起个名3 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
Dingdangcat865 小时前
城市交通多目标检测系统:YOLO11-MAN-FasterCGLU算法优化与实战应用_3
算法·目标检测·目标跟踪
专注VB编程开发20年5 小时前
C#全面超越JAVA,主要还是跨平台用的人少
java·c#·.net·跨平台
tang&5 小时前
滑动窗口:双指针的优雅舞步,征服连续区间问题的利器
数据结构·算法·哈希算法·滑动窗口
拼命鼠鼠6 小时前
【算法】矩阵链乘法的动态规划算法
算法·矩阵·动态规划
LYFlied6 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
式5166 小时前
线性代数(八)非齐次方程组的解的结构
线性代数·算法·机器学习
橘颂TA7 小时前
【剑斩OFFER】算法的暴力美学——翻转对
算法·排序算法·结构与算法
叠叠乐7 小时前
robot_state_publisher 参数
java·前端·算法
hweiyu007 小时前
排序算法:冒泡排序
算法·排序算法