C#,数值计算——索引类(Index)的计算方法与源程序

using System;

namespace Legalsoft.Truffer

{

public class Indexx

{

public int n { get; set; } = 0;

public int[] indx;

public Indexx()

{

}

public Indexx(double[] arr)

{

index(arr, arr.Length);

}

public void sort(double[] brr)

{

if (brr.Length != n)

{

throw new Exception("bad size in Index sort");

}

double[] tmp = Globals.CopyFrom(brr);

for (int j = 0; j < n; j++)

{

brr[j] = tmp[indx[j]];

}

}

public void sort(int[] brr)

{

if (brr.Length != n)

{

throw new Exception("bad size in Index sort");

}

int[] tmp = Globals.CopyFrom(brr);

for (int j = 0; j < n; j++)

{

brr[j] = tmp[indx[j]];

}

}

public double el(double[] brr, int j)

{

return brr[indx[j]];

}

public int el(int[] brr, int j)

{

return brr[indx[j]];

}

public void setEl(int[] brr, int j, int v)

{

brr[indx[j]] = v;

}

public void setEl(double[] brr, int j, double v)

{

brr[indx[j]] = v;

}

public void rank(int[] irank)

{

irank = new int[n];

for (int j = 0; j < n; j++)

{

irank[indx[j]] = j;

}

}

public void index(double[] arr, int nn)

{

const int M = 7;

const int NSTACK = 64;

int jstack = -1;

int[] istack = new int[NSTACK];

n = nn;

indx = new int[n];

int ir = n - 1;

for (int j = 0; j < n; j++)

{

indx[j] = j;

}

int l = 0;

for (; ; )

{

if (ir - l < M)

{

for (int j = l + 1; j <= ir; j++)

{

int indxt = indx[j];

double a = arr[indxt];

int i = j - 1;

for (; i >= l; i--)

{

if (arr[indx[i]] <= a)

{

break;

}

indx[i + 1] = indx[i];

}

indx[i + 1] = indxt;

}

if (jstack < 0)

{

break;

}

ir = istack[jstack--];

l = istack[jstack--];

}

else

{

int k = (l + ir) >> 1;

Globals.SWAP(ref indx[k], ref indx[l + 1]);

if (arr[indx[l]] > arr[indx[ir]])

{

Globals.SWAP(ref indx[l], ref indx[ir]);

}

if (arr[indx[l + 1]] > arr[indx[ir]])

{

Globals.SWAP(ref indx[l + 1], ref indx[ir]);

}

if (arr[indx[l]] > arr[indx[l + 1]])

{

Globals.SWAP(ref indx[l], ref indx[l + 1]);

}

int i = l + 1;

int j = ir;

int indxt = indx[l + 1];

double a = arr[indxt];

for (; ; )

{

do

{

i++;

} while (arr[indx[i]] < a);

do

{

j--;

} while (arr[indx[j]] > a);

if (j < i)

{

break;

}

Globals.SWAP(ref indx[i], ref indx[j]);

}

indx[l + 1] = indx[j];

indx[j] = indxt;

jstack += 2;

if (jstack >= NSTACK)

{

throw new Exception("NSTACK too small in index.");

}

if (ir - i + 1 >= j - l)

{

istack[jstack] = ir;

istack[jstack - 1] = i;

ir = j - 1;

}

else

{

istack[jstack] = j - 1;

istack[jstack - 1] = l;

l = i;

}

}

}

}

}

}

相关推荐
GIS小天5 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
jingling5558 分钟前
面试版-前端开发核心知识
开发语言·前端·javascript·vue.js·面试·前端框架
满分观察网友z23 分钟前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
m0_6873998427 分钟前
写一个Ununtu C++ 程序,调用ffmpeg API, 来判断一个数字电影的视频文件mxf 是不是Jpeg2000?
开发语言·c++·ffmpeg
爱上语文37 分钟前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端
A~taoker43 分钟前
taoker的项目维护(ng服务器)
java·开发语言
萧曵 丶1 小时前
Rust 中的返回类型
开发语言·后端·rust
hi星尘1 小时前
深度解析:Java内部类与外部类的交互机制
java·开发语言·交互
看到我,请让我去学习1 小时前
Qt编程-qml操作(js,c++,canvas)
开发语言·qt
橘子编程1 小时前
Python-Word文档、PPT、PDF以及Pillow处理图像详解
开发语言·python