首页 技术 正文
技术 2022年11月15日
0 收藏 414 点赞 2,162 浏览 10402 个字
 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 > );
}
上一篇: 配置https
下一篇: LeetCode - Path Sum
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848