struct
struct QSortStack {
public int high;
public int low;
}
QSortStack* stack = stackalloc QSortStack [];
unsafe static void qsort<T, U> (T[] keys, U[] items, int low0, int high0) where T : IComparable<T>
{
QSortStack* stack = stackalloc QSortStack [];
const int QSORT_THRESHOLD = ;
int high, low, mid, i, k;
int sp = ;
T key; // initialize our stack
stack[].high = high0;
stack[].low = low0; do {
// pop the stack
sp--;
high = stack[sp].high;
low = stack[sp].low; if ((low + QSORT_THRESHOLD) > high) {
// switch to insertion sort
for (i = low + ; i <= high; i++) {
for (k = i; k > low; k--) {
// if keys[k] >= keys[k-1], break
if (keys[k-] == null)
break; if (keys[k] != null && keys[k].CompareTo (keys[k-]) >= )
break; swap (keys, items, k - , k);
}
} continue;
} // calculate the middle element
mid = low + ((high - low) / ); // once we re-order the lo, mid, and hi elements to be in
// ascending order, we'll use mid as our pivot.
QSortArrange<T, U> (keys, items, low, mid);
if (QSortArrange<T, U> (keys, items, mid, high))
QSortArrange<T, U> (keys, items, low, mid); key = keys[mid]; // since we've already guaranteed that lo <= mid and mid <= hi,
// we can skip comparing them again
k = high - ;
i = low + ; do {
if (key != null) {
// find the first element with a value >= pivot value
while (i < k && key.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && key.CompareTo (keys[k]) < )
k--;
} else {
while (i < k && keys[i] == null)
i++; while (k > i && keys[k] != null)
k--;
} if (k <= i)
break; swap (keys, items, i, k); i++;
k--;
} while (true); // push our partitions onto the stack, largest first
// (to make sure we don't run out of stack space)
if ((high - k) >= (k - low)) {
if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
} if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
}
} else {
if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
} if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
}
}
} while (sp > );
} // Specialized version for items==null
unsafe static void qsort<T> (T[] keys, int low0, int high0) where T : IComparable<T>
{
QSortStack* stack = stackalloc QSortStack [];
const int QSORT_THRESHOLD = ;
int high, low, mid, i, k;
int sp = ;
T key; // initialize our stack
stack[].high = high0;
stack[].low = low0; do {
// pop the stack
sp--;
high = stack[sp].high;
low = stack[sp].low; if ((low + QSORT_THRESHOLD) > high) {
// switch to insertion sort
for (i = low + ; i <= high; i++) {
for (k = i; k > low; k--) {
// if keys[k] >= keys[k-1], break
if (keys[k-] == null)
break; if (keys[k] != null && keys[k].CompareTo (keys[k-]) >= )
break; swap (keys, k - , k);
}
} continue;
} // calculate the middle element
mid = low + ((high - low) / ); // once we re-order the lo, mid, and hi elements to be in
// ascending order, we'll use mid as our pivot.
QSortArrange<T> (keys, low, mid);
if (QSortArrange<T> (keys, mid, high))
QSortArrange<T> (keys, low, mid); key = keys[mid]; // since we've already guaranteed that lo <= mid and mid <= hi,
// we can skip comparing them again
k = high - ;
i = low + ; do {
if (key != null) {
// find the first element with a value >= pivot value
while (i < k && key.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k >= i && key.CompareTo (keys[k]) < )
k--;
} else {
while (i < k && keys[i] == null)
i++; while (k >= i && keys[k] != null)
k--;
} if (k <= i)
break; swap (keys, i, k); i++;
k--;
} while (true); // push our partitions onto the stack, largest first
// (to make sure we don't run out of stack space)
if ((high - k) >= (k - low)) {
if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
} if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
}
} else {
if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
} if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
}
}
} while (sp > );
} static bool QSortArrange<K, V> (K [] keys, V [] items, int lo, int hi, IComparer<K> comparer)
{
IComparable<K> gcmp;
IComparable cmp; if (comparer != null) {
if (comparer.Compare (keys[hi], keys[lo]) < ) {
swap<K, V> (keys, items, lo, hi);
return true;
}
} else if (keys[lo] != null) {
if (keys[hi] == null) {
swap<K, V> (keys, items, lo, hi);
return true;
} gcmp = keys[hi] as IComparable<K>;
if (gcmp != null) {
if (gcmp.CompareTo (keys[lo]) < ) {
swap<K, V> (keys, items, lo, hi);
return true;
} return false;
} cmp = keys[hi] as IComparable;
if (cmp != null) {
if (cmp.CompareTo (keys[lo]) < ) {
swap<K, V> (keys, items, lo, hi);
return true;
} return false;
}
} return false;
} // Specialized version for items==null
static bool QSortArrange<K> (K [] keys, int lo, int hi, IComparer<K> comparer)
{
IComparable<K> gcmp;
IComparable cmp; if (comparer != null) {
if (comparer.Compare (keys[hi], keys[lo]) < ) {
swap<K> (keys, lo, hi);
return true;
}
} else if (keys[lo] != null) {
if (keys[hi] == null) {
swap<K> (keys, lo, hi);
return true;
} gcmp = keys[hi] as IComparable<K>;
if (gcmp != null) {
if (gcmp.CompareTo (keys[lo]) < ) {
swap<K> (keys, lo, hi);
return true;
} return false;
} cmp = keys[hi] as IComparable;
if (cmp != null) {
if (cmp.CompareTo (keys[lo]) < ) {
swap<K> (keys, lo, hi);
return true;
} return false;
}
} return false;
} unsafe static void qsort<K, V> (K [] keys, V [] items, int low0, int high0, IComparer<K> comparer)
{
QSortStack* stack = stackalloc QSortStack [];
const int QSORT_THRESHOLD = ;
int high, low, mid, i, k;
IComparable<K> gcmp;
IComparable cmp;
int sp = ;
K key; // initialize our stack
stack[].high = high0;
stack[].low = low0; do {
// pop the stack
sp--;
high = stack[sp].high;
low = stack[sp].low; if ((low + QSORT_THRESHOLD) > high) {
// switch to insertion sort
for (i = low + ; i <= high; i++) {
for (k = i; k > low; k--) {
// if keys[k] >= keys[k-1], break
if (comparer != null) {
if (comparer.Compare (keys[k], keys[k-]) >= )
break;
} else {
if (keys[k-] == null)
break; if (keys[k] != null) {
gcmp = keys[k] as IComparable<K>;
cmp = keys[k] as IComparable;
if (gcmp != null) {
if (gcmp.CompareTo (keys[k-]) >= )
break;
} else {
if (cmp.CompareTo (keys[k-]) >= )
break;
}
}
} swap<K, V> (keys, items, k - , k);
}
} continue;
} // calculate the middle element
mid = low + ((high - low) / ); // once we re-order the low, mid, and high elements to be in
// ascending order, we'll use mid as our pivot.
QSortArrange<K, V> (keys, items, low, mid, comparer);
if (QSortArrange<K, V> (keys, items, mid, high, comparer))
QSortArrange<K, V> (keys, items, low, mid, comparer); key = keys[mid];
gcmp = key as IComparable<K>;
cmp = key as IComparable; // since we've already guaranteed that lo <= mid and mid <= hi,
// we can skip comparing them again.
k = high - ;
i = low + ; do {
// Move the walls in
if (comparer != null) {
// find the first element with a value >= pivot value
while (i < k && comparer.Compare (key, keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && comparer.Compare (key, keys[k]) < )
k--;
} else {
if (gcmp != null) {
// find the first element with a value >= pivot value
while (i < k && gcmp.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && gcmp.CompareTo (keys[k]) < )
k--;
} else if (cmp != null) {
// find the first element with a value >= pivot value
while (i < k && cmp.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && cmp.CompareTo (keys[k]) < )
k--;
} else {
while (i < k && keys[i] == null)
i++; while (k > i && keys[k] != null)
k--;
}
} if (k <= i)
break; swap<K, V> (keys, items, i, k); i++;
k--;
} while (true); // push our partitions onto the stack, largest first
// (to make sure we don't run out of stack space)
if ((high - k) >= (k - low)) {
if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
} if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
}
} else {
if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
} if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
}
}
} while (sp > );
} // Specialized version for items==null
unsafe static void qsort<K> (K [] keys, int low0, int high0, IComparer<K> comparer)
{
QSortStack* stack = stackalloc QSortStack [];
const int QSORT_THRESHOLD = ;
int high, low, mid, i, k;
IComparable<K> gcmp;
IComparable cmp;
int sp = ;
K key; // initialize our stack
stack[].high = high0;
stack[].low = low0; do {
// pop the stack
sp--;
high = stack[sp].high;
low = stack[sp].low; if ((low + QSORT_THRESHOLD) > high) {
// switch to insertion sort
for (i = low + ; i <= high; i++) {
for (k = i; k > low; k--) {
// if keys[k] >= keys[k-1], break
if (comparer != null) {
if (comparer.Compare (keys[k], keys[k-]) >= )
break;
} else {
if (keys[k-] == null)
break; if (keys[k] != null) {
gcmp = keys[k] as IComparable<K>;
cmp = keys[k] as IComparable;
if (gcmp != null) {
if (gcmp.CompareTo (keys[k-]) >= )
break;
} else {
if (cmp.CompareTo (keys[k-]) >= )
break;
}
}
} swap<K> (keys, k - , k);
}
} continue;
} // calculate the middle element
mid = low + ((high - low) / ); // once we re-order the low, mid, and high elements to be in
// ascending order, we'll use mid as our pivot.
QSortArrange<K> (keys, low, mid, comparer);
if (QSortArrange<K> (keys, mid, high, comparer))
QSortArrange<K> (keys, low, mid, comparer); key = keys[mid];
gcmp = key as IComparable<K>;
cmp = key as IComparable; // since we've already guaranteed that lo <= mid and mid <= hi,
// we can skip comparing them again.
k = high - ;
i = low + ; do {
// Move the walls in
if (comparer != null) {
// find the first element with a value >= pivot value
while (i < k && comparer.Compare (key, keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && comparer.Compare (key, keys[k]) < )
k--;
} else {
if (gcmp != null) {
// find the first element with a value >= pivot value
while (i < k && gcmp.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && gcmp.CompareTo (keys[k]) < )
k--;
} else if (cmp != null) {
// find the first element with a value >= pivot value
while (i < k && cmp.CompareTo (keys[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && cmp.CompareTo (keys[k]) < )
k--;
} else {
while (i < k && keys[i] == null)
i++; while (k > i && keys[k] != null)
k--;
}
} if (k <= i)
break; swap<K> (keys, i, k); i++;
k--;
} while (true); // push our partitions onto the stack, largest first
// (to make sure we don't run out of stack space)
if ((high - k) >= (k - low)) {
if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
} if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
}
} else {
if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
} if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
}
}
} while (sp > );
} static bool QSortArrange<T> (T [] array, int lo, int hi, Comparison<T> compare)
{
if (array[lo] != null) {
if (array[hi] == null || compare (array[hi], array[lo]) < ) {
swap<T> (array, lo, hi);
return true;
}
} return false;
} unsafe static void qsort<T> (T [] array, int low0, int high0, Comparison<T> compare)
{
QSortStack* stack = stackalloc QSortStack [];
const int QSORT_THRESHOLD = ;
int high, low, mid, i, k;
int sp = ;
T key; // initialize our stack
stack[].high = high0;
stack[].low = low0; do {
// pop the stack
sp--;
high = stack[sp].high;
low = stack[sp].low; if ((low + QSORT_THRESHOLD) > high) {
// switch to insertion sort
for (i = low + ; i <= high; i++) {
for (k = i; k > low; k--) {
if (compare (array[k], array[k-]) >= )
break; swap<T> (array, k - , k);
}
} continue;
} // calculate the middle element
mid = low + ((high - low) / ); // once we re-order the lo, mid, and hi elements to be in
// ascending order, we'll use mid as our pivot.
QSortArrange<T> (array, low, mid, compare);
if (QSortArrange<T> (array, mid, high, compare))
QSortArrange<T> (array, low, mid, compare); key = array[mid]; // since we've already guaranteed that lo <= mid and mid <= hi,
// we can skip comparing them again
k = high - ;
i = low + ; do {
// Move the walls in
if (key != null) {
// find the first element with a value >= pivot value
while (i < k && compare (key, array[i]) > )
i++; // find the last element with a value <= pivot value
while (k > i && compare (key, array[k]) < )
k--;
} else {
while (i < k && array[i] == null)
i++; while (k > i && array[k] != null)
k--;
} if (k <= i)
break; swap<T> (array, i, k); i++;
k--;
} while (true); // push our partitions onto the stack, largest first
// (to make sure we don't run out of stack space)
if ((high - k) >= (k - low)) {
if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
} if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
}
} else {
if ((k - ) > low) {
stack[sp].high = k;
stack[sp].low = low;
sp++;
} if ((k + ) < high) {
stack[sp].high = high;
stack[sp].low = k;
sp++;
}
}
} while (sp > );
}
struct的更多相关文章
- 使用struct处理二进制
有的时候需要用python处理二进制数据,比如,存取文件.socket操作时.这时候,可以使用python的struct模块来完成. struct模块中最重要的三个函数是pack(), unpack( ...
- golang struct扩展函数参数命名警告
今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...
- go-使用 unsafe 修改 struct 中的 field 的值
以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...
- C语言中struct位域的定义和使用
位域的定义和使用 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又 ...
- C# Struct结构体里数组长度的指定
typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- VC++ : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>
最近学习Google Breakpad,将其用在了自己的项目中,编译的版本为VS2010,没有什么问题.但是为了和之前的程序兼容,需要使用VS2008版本的程序,于是又编译了VS2008版本的代码,但 ...
- 字节流与数据类型的相互转换---使用struct模块
字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...
- 窥探Swift之别具一格的Struct和Class
说到结构体和类,还是那句话,只要是接触过编程的小伙伴们对这两者并不陌生.但在Swift中的Struct和Class也有着令人眼前一亮的特性.Struct的功能变得更为强大,Class变的更为灵活.St ...
- struct 大小计算
结构体是一种复合数据类型,通常编译器会自动的进行其成员变量的对齐,已提高数据存取的效率.在默认情况下,编译器为结构体的成员按照自然对齐(natural alignment)条方式分配存储空间,各个成员 ...
随机推荐
- 今天心情好,给各位免费呈上200兆SVN代码服务器一枚,不谢!
开篇先给大家讲个我自己的故事,几个月前在网上接了个小软件开发的私活,平日上班时间也比较忙,就中午一会儿休息时间能抽出来倒腾着去做点.每天下班复制一份到U盘带回去继续摸索,没多久U盘里躺着的文件列表那叫 ...
- js中的console很强大
今天闲来没事,瞎逛, 发现了淘宝首页的这个: 想起来之前在百度的 页面中也曾看到过.于是乎自己试一试. 于是便见识到了console对象其实很强大,用好它对调试真的有很大帮助. 代码: <!DO ...
- python 对象
python 对象 在python中,对象就是为C中的结构体在堆上申请的一块内存,一般来说,对象是不能被静态初始化的,并且不能再栈空间上生存.本文主要对Python的基本数据类型做简单的介绍. PyO ...
- 绝对干货:自定义msi安装包的执行过程
有时候我们需要在程序中执行另一个程序的安装,这就需要我们去自定义msi安装包的执行过程. 比如我要做一个安装管理程序,可以根据用户的选择安装不同的子产品.当用户选择了三个产品时,如果分别显示这三个产品 ...
- CSS水平垂直居中的几种方法
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- redis启动流程介绍
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/114.html?1455860562 1. 准备运行环境 * 设置oom ...
- 360路由器c301最新固件支持万能中继
360路由器c301现在已经停产了.出厂的固件是360_301_0.7.3.8.这个版本不支持中继.5G信号.本文主要介绍如何刷最新固件以及设置万能中继. 本文为作者原创,发表在博客园:http:// ...
- from表单iframe原网页嵌入
今天是巩固的from表单跟嵌入其他页面,同样的,学习到了新的知识. 温故而知新: iframe--在原页面嵌入其他页面,以窗口的样式 其中scrolling--滚动条 noresize--可调整大小 ...
- Atitit 从 RGB 到 HSL 或 HSV 的转换
Atitit 从 RGB 到 HSL 或 HSV 的转换 1.1. 从 RGB 到 HSL 或 HSV 的转换公式与原理1 1.2. public static HSV RGB2HSV(Color ...
- 关于js中的同步和异步
最近看到前端面试问到js中的同步和异步,这个问题该怎么回答? 梳理一下,js对于异步的处理,很多人的第一反应是ajax,这只能说是对了一半. 1.个人觉得,js中,最基础的异步是setTimeout和 ...