mirror of
https://github.com/Zeal-Operating-System/ZealOS.git
synced 2024-12-26 15:26:43 +00:00
150 lines
14 KiB
HTML
Executable file
150 lines
14 KiB
HTML
Executable file
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
|
|
<meta name="generator" content="ZealOS V1.07">
|
|
<style type="text/css">
|
|
body {background-color:#1f1f1f;}
|
|
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
|
|
.cF1{color:#4f84a6;background-color:#1f1f1f;}
|
|
.cF2{color:#73a255;background-color:#1f1f1f;}
|
|
.cF3{color:#297582;background-color:#1f1f1f;}
|
|
.cF4{color:#b34f4b;background-color:#1f1f1f;}
|
|
.cF5{color:#8a52c3;background-color:#1f1f1f;}
|
|
.cF6{color:#b7822f;background-color:#1f1f1f;}
|
|
.cF7{color:#444444;background-color:#1f1f1f;}
|
|
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
|
|
.cF9{color:#94bfde;background-color:#1f1f1f;}
|
|
.cFA{color:#a1ce97;background-color:#1f1f1f;}
|
|
.cFB{color:#6db4be;background-color:#1f1f1f;}
|
|
.cFC{color:#e88e88;background-color:#1f1f1f;}
|
|
.cFD{color:#ca94e8;background-color:#1f1f1f;}
|
|
.cFE{color:#d4b475;background-color:#1f1f1f;}
|
|
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<pre style="font-family:monospace;font-size:12pt">
|
|
<a name="l1"></a><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QuickSortI64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF9>I64</span><span class=cF0> e1, </span><span class=cF9>I64</span><span class=cF0> e2</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l2"></a>{</span><span class=cF2>/*Quick Sort for width==8.</span><span class=cF0>
|
|
<a name="l3"></a></span><span class=cF2>fp_compare() passes by value instead of ref.</span><span class=cF0>
|
|
<a name="l4"></a>
|
|
<a name="l5"></a></span><span class=cF2>For ascending strings: return StrCompare(e1,e2);</span><span class=cF0>
|
|
<a name="l6"></a></span><span class=cF2>For ascending ints</span><span class=cF0> </span><span class=cF2> : return e1-e2;</span><span class=cF0>
|
|
<a name="l7"></a>
|
|
<a name="l8"></a></span><span class=cF2>Maybe, look at </span><a href="https://zeal-operating-system.github.io/ZealOS/Demo/MultiCore/MPRadix.ZC.html#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.ZC</span></a><span class=cF2>.</span><span class=cF0>
|
|
<a name="l9"></a></span><span class=cF2>*/</span><span class=cF0>
|
|
<a name="l10"></a> </span><span class=cF9>I64</span><span class=cF0> i, *left, *right, pivot;
|
|
<a name="l11"></a>
|
|
<a name="l12"></a> </span><span class=cF1>if</span><span class=cF0> (num > </span><span class=cFE>1</span><span class=cF0>)
|
|
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l14"></a> left = base;
|
|
<a name="l15"></a> right = base + num - </span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l16"></a> pivot = base[num / </span><span class=cFE>2</span><span class=cF0>];
|
|
<a name="l17"></a> </span><span class=cF1>do</span><span class=cF0>
|
|
<a name="l18"></a> {
|
|
<a name="l19"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>*left, pivot</span><span class=cF7>)</span><span class=cF0> < </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l20"></a> left++;
|
|
<a name="l21"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>*right, pivot</span><span class=cF7>)</span><span class=cF0> > </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l22"></a> right--;
|
|
<a name="l23"></a> </span><span class=cF1>if</span><span class=cF0> (left <= right)
|
|
<a name="l24"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++, right--);
|
|
<a name="l25"></a> }
|
|
<a name="l26"></a> </span><span class=cF1>while</span><span class=cF0> (left <= right);
|
|
<a name="l27"></a>
|
|
<a name="l28"></a> i = right + </span><span class=cFE>1</span><span class=cF0> - base;
|
|
<a name="l29"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l30"></a> </span><span class=cF5>QuickSortI64</span><span class=cF0>(base, i, fp_compare);
|
|
<a name="l31"></a> i = base + num - left;
|
|
<a name="l32"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l33"></a> </span><span class=cF5>QuickSortI64</span><span class=cF0>(left, i, fp_compare);
|
|
<a name="l34"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l35"></a>}
|
|
<a name="l36"></a>
|
|
<a name="l37"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QuickSort2a</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> **base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> **_e1, </span><span class=cF1>U8</span><span class=cF0> **_e2</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l38"></a>{</span><span class=cF2>//Not public.For case of width==size(U8 *)==8.</span><span class=cF0>
|
|
<a name="l39"></a></span><span class=cF2>//fp_compare() passes by ref.</span><span class=cF0>
|
|
<a name="l40"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l41"></a> </span><span class=cF1>U8</span><span class=cF0> **left, **right, *pivot;
|
|
<a name="l42"></a>
|
|
<a name="l43"></a> left = base;
|
|
<a name="l44"></a> right = base + num - </span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l45"></a> pivot = base[num / </span><span class=cFE>2</span><span class=cF0>];
|
|
<a name="l46"></a> </span><span class=cF1>do</span><span class=cF0>
|
|
<a name="l47"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l48"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>left, &pivot</span><span class=cF7>)</span><span class=cF0> < </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l49"></a> left++;
|
|
<a name="l50"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>right, &pivot</span><span class=cF7>)</span><span class=cF0> > </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l51"></a> right--;
|
|
<a name="l52"></a> </span><span class=cF1>if</span><span class=cF0> (left <= right)
|
|
<a name="l53"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++, right--);
|
|
<a name="l54"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l55"></a> </span><span class=cF1>while</span><span class=cF0> (left <= right);
|
|
<a name="l56"></a>
|
|
<a name="l57"></a> i = right + </span><span class=cFE>1</span><span class=cF0> - base;
|
|
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l59"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(base, i, fp_compare);
|
|
<a name="l60"></a> i = base + num - left;
|
|
<a name="l61"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l62"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(left, i, fp_compare);
|
|
<a name="l63"></a>}
|
|
<a name="l64"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QuickSort2b</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> width, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> *e1, </span><span class=cF1>U8</span><span class=cF0> *e2</span><span class=cF7>)</span><span class=cF0>, </span><span class=cF1>U8</span><span class=cF0> *tmp)
|
|
<a name="l65"></a>{</span><span class=cF2>//Not public</span><span class=cF0>
|
|
<a name="l66"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l67"></a> </span><span class=cF1>U8</span><span class=cF0> *left, *right, *pivot = tmp + width;
|
|
<a name="l68"></a>
|
|
<a name="l69"></a> left = base;
|
|
<a name="l70"></a> right = base + (num - </span><span class=cFE>1</span><span class=cF0>) * width;
|
|
<a name="l71"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(pivot, base + num / </span><span class=cFE>2</span><span class=cF0> * width, width);
|
|
<a name="l72"></a> </span><span class=cF1>do</span><span class=cF0>
|
|
<a name="l73"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l74"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>left, pivot</span><span class=cF7>)</span><span class=cF0> < </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l75"></a> left += width;
|
|
<a name="l76"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>right, pivot</span><span class=cF7>)</span><span class=cF0> > </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l77"></a> right -= width;
|
|
<a name="l78"></a> </span><span class=cF1>if</span><span class=cF0> (left <= right)
|
|
<a name="l79"></a> {
|
|
<a name="l80"></a> </span><span class=cF1>if</span><span class=cF0> (left != right)
|
|
<a name="l81"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l82"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(tmp, right, width);
|
|
<a name="l83"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(right, left, width);
|
|
<a name="l84"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(left, tmp, width);
|
|
<a name="l85"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l86"></a> left += width;
|
|
<a name="l87"></a> right -= width;
|
|
<a name="l88"></a> }
|
|
<a name="l89"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l90"></a> </span><span class=cF1>while</span><span class=cF0> (left <= right);
|
|
<a name="l91"></a>
|
|
<a name="l92"></a> i = </span><span class=cFE>1</span><span class=cF0> + (right - base) / width;
|
|
<a name="l93"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l94"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(base, i, width, fp_compare, tmp);
|
|
<a name="l95"></a> i = num + (base - left) / width;
|
|
<a name="l96"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> < i < num)
|
|
<a name="l97"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(left, i, width, fp_compare, tmp);
|
|
<a name="l98"></a>}
|
|
<a name="l99"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QuickSort</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> width, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> *e1, </span><span class=cF1>U8</span><span class=cF0> *e2</span><span class=cF7>)</span><span class=cF0>)
|
|
<a name="l100"></a>{</span><span class=cF2>/*Quick Sort: fp_compare() passes by ref.</span><span class=cF0>
|
|
<a name="l101"></a>
|
|
<a name="l102"></a></span><span class=cF2>For ascending strings: return StrCompare(*e1,*e2);</span><span class=cF0>
|
|
<a name="l103"></a></span><span class=cF2>For ascending ints</span><span class=cF0> </span><span class=cF2> : return *e1-*e2;</span><span class=cF0>
|
|
<a name="l104"></a></span><span class=cF2>Don't return e1-e2 if numbers can overflow, return -1,0 or 1.</span><span class=cF0>
|
|
<a name="l105"></a>
|
|
<a name="l106"></a></span><span class=cF2>Maybe, look at </span><a href="https://zeal-operating-system.github.io/ZealOS/Demo/MultiCore/MPRadix.ZC.html#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.ZC</span></a><span class=cF2>.</span><span class=cF0>
|
|
<a name="l107"></a></span><span class=cF2>*/</span><span class=cF0>
|
|
<a name="l108"></a> </span><span class=cF1>U8</span><span class=cF0> *tmp;
|
|
<a name="l109"></a>
|
|
<a name="l110"></a> </span><span class=cF1>if</span><span class=cF0> (width && num > </span><span class=cFE>1</span><span class=cF0>)
|
|
<a name="l111"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l112"></a> </span><span class=cF1>if</span><span class=cF0> (width == </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF2>//assign instead of MemCopy for width 8</span><span class=cF0>
|
|
<a name="l113"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(base, num, fp_compare);
|
|
<a name="l114"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l115"></a> {
|
|
<a name="l116"></a> tmp = </span><span class=cF5>MAlloc</span><span class=cF0>(width * </span><span class=cFE>2</span><span class=cF0>);
|
|
<a name="l117"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(base, num, width, fp_compare, tmp);
|
|
<a name="l118"></a> </span><span class=cF5>Free</span><span class=cF0>(tmp);
|
|
<a name="l119"></a> }
|
|
<a name="l120"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l121"></a>}
|
|
</span></pre></body>
|
|
</html>
|